Function Repository Resource:

RandomDotOrgInteger

Source Notebook

Generate a random integer using the random.org service

Contributed by: Jan Mangaldan

ResourceFunction["RandomDotOrgInteger"][{imin,imax}]

gives a random integer in the range {imin,imax} using the random.org service.

ResourceFunction["RandomDotOrgInteger"][imax]

gives a random integer in the range {0,,imax}.

ResourceFunction["RandomDotOrgInteger"][]

randomly gives 0 or 1.

ResourceFunction["RandomDotOrgInteger"][range,n]

gives a list of n random integers.

ResourceFunction["RandomDotOrgInteger"][range,{n1,n2,}]

gives an n1×n2×… array of random integers.

Details and Options

random.org is a true random number service that generates randomness via atmospheric noise, which for many purposes is better than the pseudorandom number algorithms typically used in computer programs.
ResourceFunction["RandomDotOrgInteger"] gives a different sequence of random integers whenever it is evaluated.
It is more efficient to generate multiple random numbers in a single call (e.g. ResourceFunction["RandomDotOrgInteger"][range,n]) instead of generating them one at a time (e.g. Table[ResourceFunction["RandomDotOrgInteger"][range],{n}]).

Examples

Basic Examples (5) 

A random integer in the range 1 through 10:

In[1]:=
ResourceFunction["RandomDotOrgInteger"][{1, 10}]
Out[1]=

A random integer in the range 0 through 3:

In[2]:=
ResourceFunction["RandomDotOrgInteger"][3]
Out[2]=

A random choice of 0 or 1:

In[3]:=
ResourceFunction["RandomDotOrgInteger"][]
Out[3]=

Twenty random integers in the range 0 through 5:

In[4]:=
ResourceFunction["RandomDotOrgInteger"][5, 20]
Out[4]=

A 3×4 random array of 0s and 1s:

In[5]:=
ResourceFunction["RandomDotOrgInteger"][1, {3, 4}]
Out[5]=

Applications (4) 

A cellular automaton with random initial conditions:

In[6]:=
ArrayPlot[
 CellularAutomaton[110, ResourceFunction["RandomDotOrgInteger"][1, 200], 100]]
Out[6]=

Random circles at integer positions:

In[7]:=
Graphics[
 Circle /@ ResourceFunction["RandomDotOrgInteger"][10, {100, 2}]]
Out[7]=

Random array of black and white cells:

In[8]:=
ArrayPlot[ResourceFunction["RandomDotOrgInteger"][1, {30, 40}]]
Out[8]=

Count how many pairs of random integers between 1 and a million are relatively prime:

In[9]:=
Count[GCD @@@ ResourceFunction["RandomDotOrgInteger"][{1, 1*^6}, {1000, 2}], 1]
Out[9]=

Possible Issues (1) 

Integers outside the range {-109,109} cannot be generated:

In[10]:=
ResourceFunction["RandomDotOrgInteger"][10^10]
Out[10]=

Neat Examples (1) 

A randomly filled cubic lattice:

In[11]:=
Graphics3D[
 Sphere /@ (2 ResourceFunction["RandomDotOrgInteger"][5, {200, 3}])]
Out[11]=

Version History

  • 1.0.0 – 09 January 2023

Related Resources

Author Notes

The implementation is adapted from https://tpfto.wordpress.com/2012/05/15/using-the-random-org-generator-in-mathematica/

License Information