Macaulay2 » Documentation
Packages » Macaulay2Doc :: setRandomSeed
next | previous | forward | backward | up | index | toc

setRandomSeed -- set starting point for random number generator

Synopsis

Description

Since version 1.2, when Macaulay2 starts the random number seed is initially set to a number that depends on the current date, the time (in seconds), and the process id, except for when running examples and tests in packages, where it is always initialized to 0 by passing the command line option --no-randomize.

The sequence of future pseudo-random results is determined by the random number seed.

i1 : setRandomSeed()
i2 : random 2^100

o2 = 278091837517481385900793178228
i3 : random 2^100

o3 = 1096904973926824930696315518408
i4 : setRandomSeed()
i5 : random 2^100

o5 = 278091837517481385900793178228
i6 : random 2^100

o6 = 1096904973926824930696315518408

If an integer seed is given, the random number seed is set to the low-order 32 bits of the seed.

i7 : setRandomSeed 123456

o7 = 123456
i8 : for i to 10 list random 100

o8 = {8, 29, 5, 22, 4, 32, 35, 57, 3, 95, 36}

o8 : List
i9 : setRandomSeed 123456

o9 = 123456
i10 : for i to 10 list random 100

o10 = {8, 29, 5, 22, 4, 32, 35, 57, 3, 95, 36}

o10 : List

If a string seed is given, sets the random number seed to an integer computed from it. Every character of the string contributes to the seed, but only 32 bits of data are used. The sequence of future pseudo-random results is determined by the seed.

i11 : setRandomSeed "thrkwjsxz"

o11 = 1267386136561992923
i12 : for i to 10 list random 100

o12 = {28, 70, 67, 31, 68, 14, 42, 24, 16, 14, 51}

o12 : List
i13 : setRandomSeed "thrkwjsxz"

o13 = 1267386136561992923
i14 : for i to 10 list random 100

o14 = {28, 70, 67, 31, 68, 14, 42, 24, 16, 14, 51}

o14 : List

Code

../../../../../Macaulay2/m2/system.m2:139:25-139:41: --source code:
setRandomSeed ZZ := seed -> randomSeed = seed               -- magic assignment, calls rawSetRandomSeed internally
../../../../../Macaulay2/m2/system.m2:140:29-140:80: --source code:
setRandomSeed String := seed -> setRandomSeed fold((i,j) -> 101*i + j, 0, ascii seed)
../../../../../Macaulay2/m2/system.m2:141:31-141:100: --source code:
setRandomSeed Sequence := seed -> if seed === () then rawRandomInitialize() else setRandomSeed hash seed

Ways to use setRandomSeed :

For the programmer

The object setRandomSeed is a method function with a single argument.