Skip to main content
saved 1 byte
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650

JavaScript (ES7), 3837 bytes

-1 thanks to @Neil

n=>(Math.random()*n*-~n+2&~1~n+1|0)**.5+.5|0

Try it online!Try it online!

How?

We essentially pick a random integer in \$\left[1\dots\sum_{k=1}^{n}k\right]\$ and then return the corresponding term in the sequence \$1,2,2,3,3,3,4,4,4,4,\dots\$ (this is A002024).

A more readable form of the formula is:

$$\left\lfloor\sqrt{2 \times\left\lfloor \operatorname{rand}()\times {n+1\choose 2}+1 \right\rfloor }+1/2\right\rfloor$$

leading to:

$$\left\lfloor\sqrt{2 \times\lfloor \operatorname{rand}()\times n\times(n+1)/2+1 \rfloor }+1/2\right\rfloor$$

where \$\operatorname{rand}()\$ is assumed to return a random value drawn from the uniform distribution in the interval \$[0,1[\$.

JavaScript (ES7), 38 bytes

n=>(Math.random()*n*-~n+2&~1)**.5+.5|0

Try it online!

How?

We essentially pick a random integer in \$\left[1\dots\sum_{k=1}^{n}k\right]\$ and then return the corresponding term in the sequence \$1,2,2,3,3,3,4,4,4,4,\dots\$ (this is A002024).

A more readable form of the formula is:

$$\left\lfloor\sqrt{2 \times\left\lfloor \operatorname{rand}()\times {n+1\choose 2}+1 \right\rfloor }+1/2\right\rfloor$$

leading to:

$$\left\lfloor\sqrt{2 \times\lfloor \operatorname{rand}()\times n\times(n+1)/2+1 \rfloor }+1/2\right\rfloor$$

where \$\operatorname{rand}()\$ is assumed to return a random value drawn from the uniform distribution in the interval \$[0,1[\$.

JavaScript (ES7), 37 bytes

-1 thanks to @Neil

n=>(Math.random()*n*-~n+1|0)**.5+.5|0

Try it online!

How?

We essentially pick a random integer in \$\left[1\dots\sum_{k=1}^{n}k\right]\$ and then return the corresponding term in the sequence \$1,2,2,3,3,3,4,4,4,4,\dots\$ (this is A002024).

A more readable form of the formula is:

$$\left\lfloor\sqrt{2 \times\left\lfloor \operatorname{rand}()\times {n+1\choose 2}+1 \right\rfloor }+1/2\right\rfloor$$

leading to:

$$\left\lfloor\sqrt{2 \times\lfloor \operatorname{rand}()\times n\times(n+1)/2+1 \rfloor }+1/2\right\rfloor$$

where \$\operatorname{rand}()\$ is assumed to return a random value drawn from the uniform distribution in the interval \$[0,1[\$.

minor update
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650

JavaScript (ES7), 38 bytes

n=>(Math.random()*n*-~n+2&~1)**.5+.5|0

Try it online!

How?

We essentially pick a random integer in \$\left[1\dots\sum_{k=1}^{n}k\right]\$ and then return the corresponding term in the sequence \$1,2,2,3,3,3,4,4,4,4,\dots\$ (this is A002024).

A more readable form of the formula is:

$$\left\lfloor\sqrt{2 \times\left\lfloor \operatorname{rand}()\times {n+1\choose 2}+1 \right\rfloor }+1/2\right\rfloor$$

leading to:

$$\left\lfloor\sqrt{2 \times\lfloor \operatorname{rand}()\times n\times(n+1)/2+1 \rfloor }+1/2\right\rfloor$$

where \$\operatorname{rand}()\$ is assumed to return a random value drawn from the uniform distribution in the interval \$[0,1[\$.

JavaScript (ES7), 38 bytes

n=>(Math.random()*n*-~n+2&~1)**.5+.5|0

Try it online!

How?

A more readable form of the formula is:

$$\left\lfloor\sqrt{2 \times\left\lfloor \operatorname{rand}()\times {n+1\choose 2}+1 \right\rfloor }+1/2\right\rfloor$$

leading to:

$$\left\lfloor\sqrt{2 \times\lfloor \operatorname{rand}()\times n\times(n+1)/2+1 \rfloor }+1/2\right\rfloor$$

where \$\operatorname{rand}()\$ is assumed to return a random value drawn from the uniform distribution in the interval \$[0,1[\$.

JavaScript (ES7), 38 bytes

n=>(Math.random()*n*-~n+2&~1)**.5+.5|0

Try it online!

How?

We essentially pick a random integer in \$\left[1\dots\sum_{k=1}^{n}k\right]\$ and then return the corresponding term in the sequence \$1,2,2,3,3,3,4,4,4,4,\dots\$ (this is A002024).

A more readable form of the formula is:

$$\left\lfloor\sqrt{2 \times\left\lfloor \operatorname{rand}()\times {n+1\choose 2}+1 \right\rfloor }+1/2\right\rfloor$$

leading to:

$$\left\lfloor\sqrt{2 \times\lfloor \operatorname{rand}()\times n\times(n+1)/2+1 \rfloor }+1/2\right\rfloor$$

where \$\operatorname{rand}()\$ is assumed to return a random value drawn from the uniform distribution in the interval \$[0,1[\$.

minor update
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650

JavaScript (ES7), 38 bytes

n=>(Math.random()*n*-~n+2&~1)**.5+.5|0

Try it online!

How?

A more readable form of the formula is:

$$\left\lfloor\sqrt{2 \times\left\lfloor \operatorname{rand}()\times {n+1\choose 2}+1 \right\rfloor }+1/2\right\rfloor$$

leading to:

$$\left\lfloor\sqrt{2 \times\lfloor \operatorname{rand}()\times n\times(n+1)/2+1 \rfloor }+1/2\right\rfloor$$

where \$\operatorname{rand}()\$ is assumed to return a random value drawn from the uniform distribution in the interval \$[0,1[\$.

JavaScript (ES7), 38 bytes

n=>(Math.random()*n*-~n+2&~1)**.5+.5|0

Try it online!

How?

A more readable form of the formula is:

$$\left\lfloor\sqrt{2 \times\lfloor \operatorname{rand}()\times n\times(n+1)/2+1 \rfloor }+1/2\right\rfloor$$

where \$\operatorname{rand}()\$ is assumed to return a random value drawn from the uniform distribution in the interval \$[0,1[\$.

JavaScript (ES7), 38 bytes

n=>(Math.random()*n*-~n+2&~1)**.5+.5|0

Try it online!

How?

A more readable form of the formula is:

$$\left\lfloor\sqrt{2 \times\left\lfloor \operatorname{rand}()\times {n+1\choose 2}+1 \right\rfloor }+1/2\right\rfloor$$

leading to:

$$\left\lfloor\sqrt{2 \times\lfloor \operatorname{rand}()\times n\times(n+1)/2+1 \rfloor }+1/2\right\rfloor$$

where \$\operatorname{rand}()\$ is assumed to return a random value drawn from the uniform distribution in the interval \$[0,1[\$.

added an explanation
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650
Loading
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650
Loading