Just ran into a something interesting in Javascript while experimenting with generating a random number in the condition (is that what it's called?) of a for loop.
So, if I were to write the code like this:
for (var i = 0; i < 20; i++) {
var count = 0;
for (var j = 0; j < Math.floor(Math.random() * 20); j++) {
count++;
}
console.log(count);
}
It would return a result like this:
9
5
8
3
3
2
6
8
4
4
5
6
3
3
5
3
4
5
3
11
But if I were to generate a random number in a variable before the second for loop:
for (var i = 0; i < 20; i++) {
var count = 0;
var loopEnd = Math.floor(Math.random() * 20 + 1);
for (var j = 0; j < loopEnd; j++) {
count++;
}
console.log(count);
}
It would return a result like this:
11
13
14
2
19
19
17
19
2
18
5
15
18
2
1
19
16
15
13
20
What exactly is happening here? It had me confused for a little while. Is the Math.random() inside the for loop generating a new random number after each iteration? Does the loop run the code, iterate, and check the condition, and generate a new random number each time it is checking the condition? Is this what is happening and is this why the numbers in the console are smaller than if I use Math.random() in a variable before the for loop?
To perform this operation we are using the srand() function. This is in the C++ library. The function void srand(unsigned int seed) seeds the random number generator used by the function rand. It takes a parameter called seed.
Javascript creates pseudo-random numbers with the function Math. random() . This function takes no parameters and creates a random decimal number between 0 and 1. The returned value may be 0, but it will never be 1.
Random num = new Random(); Now, in a loop, use the nextInt() method since it is used to get the next random integer value. You can also set a range, like for 0 to 20, write it as. nextInt( 20 );
The JavaScript Math. random() method is an excellent built-in method for producing random numbers. When Math. random() is executed, it returns a random number that can be anywhere between 0 and 1.
In the first example's inner loop:
...
for (var j = 0; j < Math.floor(Math.random() * 20); j++) {
count++;
}
...
the termination condition j < Math.floor(Math.random() * 20)
is evaluated each iteration of the loop, and so the termination value is changing each iteration with the call to Math.random()
making it more likely that the loop will terminate earlier.
As for the loop terminating earlier on average when the random number is chosen anew every iteration:
Without getting into proper statistical analysis, consider this simple train of thought:
How likely are you to get ten or more iterations?
If you draw just one random number between 1 and 20, you'll get 10 or more about 50% of the time.
If you get ten random numbers you need to have each of them higher than first 1, then 2, then 3 and so on, and the odds are reduced: 95% * 90% * 85% * ... * 50%.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With