Function Repository Resource:

RandomComposite

Source Notebook

Find a random composite number or set of random composite numbers

Contributed by: Peter Cullen Burbery

ResourceFunction["RandomComposite"][n]

returns a random composite number up to n.

ResourceFunction["RandomComposite"][n,m]

returns m random composite numbers up to n.

ResourceFunction["RandomComposite"][{min,max}]

returns a random composite number between min and max.

ResourceFunction["RandomComposite"][n, arrayspec]

returns an array of random composite numbers up to n.

Details

RandomComposite can return duplicates.

Examples

Basic Examples (2) 

Find a random composite number less than or equal to the a ceiling of the exponential of an integer:

In[1]:=
ResourceFunction["RandomComposite"][55]
Out[1]=

Find a random composite number between 149 and 404:

In[2]:=
ResourceFunction["RandomComposite"][{149, 404}]
Out[2]=

Scope (5) 

Find a list of 30 random composite numbers between 1097 and 2981:

In[3]:=
ResourceFunction["RandomComposite"][{1097, 2981}, 30]
Out[3]=

Find a 3⨯3 matrix of random composite numbers between 8104 and 22027 and print it in matrix form:

In[4]:=
ResourceFunction[
  "RandomComposite"][{8104, 22027}, {3, 3}] // MatrixForm
Out[4]=

Find a 3⨯3⨯3 array of random composite numbers between 59875 and 162755 and print it in matrix form:

In[5]:=
ResourceFunction[
  "RandomComposite"][{59875, 162755}, {3, 3, 3}] // MatrixForm
Out[5]=

Find a 3⨯3⨯3⨯3 tensor of random composite numbers between 442414 and 1202605:

In[6]:=
ResourceFunction[
  "RandomComposite"][{59875, 162755}, {3, 3, 3, 3}] // MatrixForm
Out[6]=

Find a large random composite up to a googol:

In[7]:=
ResourceFunction["RandomComposite"][10^100]
Out[7]=

Possible Issues (2) 

The function can return duplicates:

In[8]:=
ResourceFunction["RandomComposite"][{2, 5}, 7]
Out[8]=

This is similar to the behavior of RandomPrime, which also produces duplicates:

In[9]:=
RandomPrime[{8, 12}, 7]
Out[9]=

Publisher

Peter Burbery

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.1 – 28 July 2023
  • 1.0.0 – 06 June 2023

Related Resources

Author Notes

There was a bug where the function would only work if the min was greater than or equal to 4. For example, neither RandomComposite[{2,5}] nor RandomComposite[{3,5}] would work. Now I have fixed it so it works as long as the min is greater than 0.

License Information