I need to display an oddinput
random number within the range I set. I tried using a for loop with an if , but my program "thunders" .
This is what I have for now:
var min = 200;
var max = 300;
var semrandom;
semrandom = Math.floor(Math.random() * max) + min;
if ((semrandom % 2) == 0) {
continue;
}
document.getElementById('semilla').setAttribute("value", `${semrandom}`);
If it generates a random number, but not an odd random number . The question is, how can I get it?
There are cases where the use of recursion is unnecessary. This is one of them. Odd numbers have the form 2n + 1 so if n is a random number then so is 2n + 1 and it will be a random odd . Using this elementary principle, the solution to obtain an odd random number in the desired range (200 to 300) is as follows:
From there it is very easy to make a generalization for arbitrary ranges.
General solution:As I offered a while ago (in the comments) a general function that applies the idea would be the following:
The error is due to the fact that the instruction
continue
is used to skip the iteration of a repetitive cycle, since it does not have a cycle (for/while for example) it gives you the errorOnce this is clear, what if we think a bit like mathematicians, the formula for an odd number is:
Where
2X
it guarantees us that the number is even, and then we add 1 to make it odd in our case, we must add one when it is evenTo optimize the code we will rely on an example of the crazy guy from @Trauma.JS to know what number is even/odd , we apply other characteristics and we have the following:
The idea of
!(X & 1)
this returns me atrue
if the number is even as it is even and prior to this there is a Javascript sum change ittrue
to1
Find odd random numbers
One possible solution to find odd random numbers is by using recursion . Therefore, you must create a function that will be called oddRandom().
For example:
Which will have the following instructions implemented so that it looks something like this:
Once prepared, we will implement it in a similar way to this:
Where min and max are the intervals comprised of random numbers that we want to obtain.
Play example:
In order not to make this answer too long, we will create a reproducible example in which you can test it and implement it directly in your project:
From the example above, you only need to take into account the JavaScript code , which is what produces the desired result. After all, the numbers needed are the odd random numbers .
The same example can be reproduced in your text box like this:
It is not necessary to place
document.getElementById("semilla")
if theid
of the text box isid="semilla"
.Means:
The code you provide seems to be the solution however the use of the reserved word
continue
within a simple conditional structure returns this error:It seems to me that if you want to control the actions that your code will take depending on whether it is even or odd, then we can place it inside a function, so that if the number obtained is even, we do a
return false;
and otherwise we can, for example, print said value.return
is only possible within a function contextcontinue
can only be expressed within the context of a conditional that exists within the body of a loop; for further reference read hereExample
The correct function to get a random number between two integer intervals greater than zero is
Math.floor((Math.random() * (max - min + 1)) + min)
you will see that your function goes out of the maximum range. In addition, no recursion or anything similar is necessary because an odd number is always obtained from an even number if we add or subtract 1 to it.With
Math.floor((Math.random() * max) + min);
the maximum it's actually 499, 299 + 200, becauseMath.random()
it's never 1 so the first part can never be 300.On the other hand,
max - min + 1
it gives us the range of numbers we are restricted to, the +1 is to include the missing number becauseMath.random()
it is never 1.+min
It is to run the rangemin
times.