I currently have code to randomly generate values from 1-10 but I want to increase the probability of generating 10 by 3. How do I go about changing up my code?
Asked
Active
Viewed 53 times
2 Answers
1
You can create an array of numbers between your range, an repeat which num you want to increase his chance.
For example: [1,2,3,4,5,6,7,8,9,10,10,10,10,10,10,10,10,10].
Now generate random number in array length and you have 0.5 probability to 10.
And each other number has probability of 1/18.

Yosef.Schwartz
- 79
- 6
-1
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10 10]
myRandomValue = array[Math.floor(Math.random() * array.length)]
If you're running this a bunch of times, you might want to compute and store array.length
once.

doctorjay
- 91
- 6