You may throw the die many () times, take the sum of the outcomes and consider the residue class
. The distribution on
gets closer and closer to a uniform distribution as
increases.
You may throw the die once to decide if the final outcome will be even or odd, then throw it again until it gives an outcome different from six, that fixes the residue class . In such a way you generate a uniform distribution over
with
tosses, in average.
If you are allowed to throw the die in a wedge, you may label the edges of the die with the numbers in and mark two opposite edges as "reroll". In such a way you save exactly one toss, and need just
tosses, in average.
Obviously, if you are allowed to throw the die in decagonal glass you don't even need the die, but in such a case the lateral thinking spree ends with just toss. Not much different from buying a D10, as Travis suggested.
At last, just for fun: look at the die, without throwing it. Then look at your clock, the last digit of the seconds. Add one. tosses.
Generate random number between two numbers in JavaScript - Stack Overflow
Creating a random number within a defined range using JavaScript
Set variable with random number
Generating random number between 1 to 10
How random is this Random Number Generator?
Do you have a Random Number Generator mobile app?
What is the maximum number you can use in your Number Generator?
Videos
You may throw the die many () times, take the sum of the outcomes and consider the residue class
. The distribution on
gets closer and closer to a uniform distribution as
increases.
You may throw the die once to decide if the final outcome will be even or odd, then throw it again until it gives an outcome different from six, that fixes the residue class . In such a way you generate a uniform distribution over
with
tosses, in average.
If you are allowed to throw the die in a wedge, you may label the edges of the die with the numbers in and mark two opposite edges as "reroll". In such a way you save exactly one toss, and need just
tosses, in average.
Obviously, if you are allowed to throw the die in decagonal glass you don't even need the die, but in such a case the lateral thinking spree ends with just toss. Not much different from buying a D10, as Travis suggested.
At last, just for fun: look at the die, without throwing it. Then look at your clock, the last digit of the seconds. Add one. tosses.
Write out the base- decimals of
through
.
$$\begin{array}{cc} \frac{0}{10} & = 0.00000000\dots\\ \frac{1}{10} & = 0.03333333\dots\\ \frac{2}{10} & = 0.11111111\dots\\ \frac{3}{10} & = 0.14444444\dots\\ \frac{4}{10} & = 0.22222222\dots\\ \frac{5}{10} & = 0.30000000\dots\\ \frac{6}{10} & = 0.33333333\dots\\ \frac{7}{10} & = 0.41111111\dots\\ \frac{8}{10} & = 0.44444444\dots\\ \frac{9}{10} & = 0.52222222\dots\\ \frac{10}{10} & = 1.00000000\dots\\ \end{array}$$
Treat rolls of a as a
. As you roll your
-sided die, you are generating digits of a base-
decimal number, uniformly distributed between
and
. There are
gaps in between the fractions for
, corresponding to the
uniformly random outcomes you are looking for. You know which outcome you are generating as soon as you know which gap the random number will be in.
This is kind of annoying to do. Here's an equivalent algorithm:
Roll a die
![]()
: Roll a die
![]()
: Roll a die
![]()
: Roll a die
One sees that:
returns
with probability
and
with probability
.
returns
with probability
,
with probability
, and
with probability
.
returns
with probability
and
with probability
.
Overall, it produces the outcomes each with
probability.
Procedures and
are expected to require
rolls. Procedure
is expected to require
rolls. So the main procedure is expected to require
rolls.
function randomIntFromInterval(min, max) { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min);
}
const rndInt = randomIntFromInterval(1, 6);
console.log(rndInt);
What it does "extra" is it allows random intervals that do not start with 1. So you can get a random number from 10 to 15 for example. Flexibility.
Important
The following code works only if the minimum value is `1`. It does not work for minimum values other than `1`.If you wanted to get a random integer between 1 (and only 1) and 6, you would calculate:
const rndInt = Math.floor(Math.random() * 6) + 1
console.log(rndInt)
Where:
- 1 is the start number
- 6 is the number of possible results (1 + start (6) - end (1))
Hi all,
I'm learning JS through FCC and it's going okay so far. I'm not a very mathematically inclined person, however, so I struggle with some concepts.
I'm struggling to understand how the following code works;
Math.floor(Math.random() * (max - min + 1)) + min
I tried writing out an example on paper and used 10 and 5 as max and min respectively.
Math.floor(Math.random() * (10 - 5 + 1)) + 5
wouldn't this mean that I can get numbers higher than the max though? Say I get a randomly generated 0.99; well 0.99 * (6) + 5 = 10.94 which is greater than my max of 10?
Am I doing this wrong?