I want a javascript script that choose either value1 or value2 randomly, not between the two values , just the actual values.
Thanks!!!!
Example: Integer Value Between Two Numbers In JavaScript, you can generate a random number with the Math. random() function.
The Excel RANDBETWEEN function returns a random integer between given numbers. RANDBETWEEN is a volatile function recalculates when a worksheet is opened or changed. This formula is then copied down from B5 to B11. The result is random numbers between 1-100.
To check if a number is between two numbers: Use the && (and) operator to chain two conditions. In the first condition check that the number is greater than the lower range and in the second, that the number is lower than the higher range. If both conditions are met, the number is in the range.
random() * items. length); items[index] = whatever; If you only have a couple variables, you can generate a random number and use an if/else statement to operate on the desired variable.
The Math.random
[MDN] function chooses a random value in the interval [0, 1)
. You can take advantage of this to choose a value randomly.
var chosenValue = Math.random() < 0.5 ? value1 : value2;
Math.round(Math.random())
returns a 0 or a 1, each value just about half the time.
You can use it like a true or false, 'heads' or 'tails', or as a 2 member array index-
['true','false'][Math.round(Math.random())]
will return 'true' or 'false'...
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