Skip to content

Random

The random package provides several functions for generating random numbers. It uses a cryptographically strong pseudorandom number generator when possible, but falls back to a weaker random number generator when cryptographically strong randomness is not available (on older browsers or on servers that don't have enough entropy to seed the cryptographically strong generator).

Random.id

Summary:

Return a unique identifier, such as "Jjwjg6gouWLXhMGKW", that is likely to be unique in the whole world.

Arguments:

Source code
NameTypeDescriptionRequired
nNumber

Optional length of the identifier in characters (defaults to 17)

No
js
import { Random } from "meteor/random";


const result = Random.id();
  42
);

Random.secret

Summary:

Return a random string of printable characters with 6 bits of entropy per character. Use Random.secret for security-critical secrets that are intended for machine, rather than human, consumption.

Arguments:

Source code
NameTypeDescriptionRequired
nNumber

Optional length of the secret string (defaults to 43 characters, or 256 bits of entropy)

No
js
import { Random } from "meteor/random";


const result = Random.secret();
  42
);

Random.fraction

Summary:

Return a number between 0 and 1, like Math.random.

Random.choice

Summary:

Return a random element of the given array or string.

Arguments:

Source code
NameTypeDescriptionRequired
arrayOrStringArray or String

Array or string to choose from

Yes
js
import { Random } from "meteor/random";


const result = Random.choice();
  []
);

Random.hexString

Summary:

Return a random string of n hexadecimal digits.

Arguments:

Source code
NameTypeDescriptionRequired
nNumber

Length of the string

Yes
js
import { Random } from "meteor/random";


const result = Random.hexString();
  42
);