CobaRandom

class coba.random.CobaRandom

A random number generator.

Constructors

__init__(seed: float | None = None) None

Instantiate a CobaRandom generator.

Parameters:

seed – The seed to use when starting random number generation.

Remarks:

The values for a,c,m below are taken from L’ecuyer (1999). In this paper he notes that these values for LCG should have an overall period of m though the period of lower bits will be much shorter. A solution he offers to this problem is to use much larger m (e.g., 2**128) and then only use the the top n most significant digits. For now, we aren’t doing that.

References

L’ecuyer, Pierre. “Tables of linear congruential generators of different sizes and good lattice structure.” Mathematics of Computation 68.225 (1999): 249-260.

Methods

choice(seq: Sequence[Any], weights: Sequence[float] | None = None) Any | Tuple[Any, float]

Choose a random item from the given sequence.

Parameters:
  • seq – The sequence to pick randomly from.

  • weights – The frequency which seq is selected.

Returns:

An item in seq.

gauss(mu: float = 0, sigma: float = 1) float

Generate a random number from N(mu,sigma).

Parameters:
  • mu – The expectation of the distribution we are drawing from.

  • sigma – The standard deviation of the distribution we are drawing form.

Returns:

A random number drawn from N(mu,sigma).

gausses(n: int, mu: float = 0, sigma: float = 1) Sequence[float]

Generate n independent random numbers from N(mu,sigma).

Parameters:
  • n – The number of random numbers to generate.

  • mu – The expectation of the distribution we are drawing from.

  • sigma – The standard deviation of the distribution we are drawing form.

Returns:

The n random numbers drawn from N(mu,sigma).

randint(a: int, b: int) int

Generate a uniform random integer in [a, b].

Parameters:
  • a – The inclusive lower bound for the random integer.

  • b – The inclusive upper bound for the random integer.

Returns:

A random integer in [a,b].

randints(n: int, a: int, b: int) Sequence[int]

Generate n uniform random integers in [a, b].

Parameters:
  • n – The number of random integers to generate.

  • a – The inclusive lower bound for the random integer.

  • b – The inclusive upper bound for the random integer.

Returns:

A sequence of n random integers in [a,b].

random(min: float = 0, max: float = 1) float

Generate a uniform random number in [min,`max`).

Parameters:
  • min – The minimum value for the random numbers.

  • max – The maximum value for the random numbers.

Returns:

The generated random number in [min,`max`).

randoms(n: int, min: float = 0, max: float = 1) Sequence[float]

Generate n uniform random numbers in [min,`max`).

Parameters:
  • n – How many random numbers should be generated.

  • min – The minimum value for the random numbers.

  • max – The maximum value for the random numbers.

Returns:

The n generated random numbers in [min,`max`).

shuffle(items: Iterable[Any], inplace: bool = False) Sequence[Any]

Shuffle the order of items in a sequence.

Parameters:
  • items – The items that are to be shuffled.

  • inplace – Shuffle the items in their given container.

Remarks:

This is the Richard Durstenfeld’s method popularized by Donald Knuth in The Art of Computer Programming. This algorithm is unbiased (i.e., all possible permutations are equally likely to occur).

Returns:

A new order of the original items.