NewGenerator - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

RandomTools[LinearCongruence]

 NewGenerator
 Create a Linear Congruence Pseudo Random Number Generator

 Calling Sequence NewGenerator( opt1, opt2, ... )

Parameters

 opt1, opt2, ... - (optional) argument of the form option=value where option is range

Description

 • The NewGenerator command outputs a Maple procedure, a pseudo-random number generator, which when called outputs one pseudo-random integer. The output of the generator depends on the options described below. The default is to output integers on the range $0..999999999999$, i.e., a random 12 digit integer.
 • The returned procedure calls the LinearCongruence algorithm to generate the numbers.  Although you can have multiple generating procedures, they all share the same state.  This means that calling one procedure will effect the numbers returned by another.
 • The following optional arguments are supported. They are input as equations in any order.
 • range=integer..integer or integer
 If the value of the range argument is a range, then the integer will be chosen from that range.  If the value of the range argument is an integer, then the integer will be take from the range [0..value).  The default range is $1000000000000$.
 • If one only needs to generate a small number of integers then the GenerateInteger function can be used.  However, using a procedure returned by NewGenerator is faster than calling GenerateInteger multiple times.

Examples

 > $\mathrm{with}\left(\mathrm{RandomTools}\left[\mathrm{LinearCongruence}\right]\right)$
 $\left[{\mathrm{GenerateInteger}}{,}{\mathrm{GetState}}{,}{\mathrm{NewGenerator}}{,}{\mathrm{SetState}}\right]$ (1)
 > $M≔\mathrm{NewGenerator}\left(\mathrm{range}=1..6\right)$
 ${M}{≔}{\mathbf{proc}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{LinearCongruence}}{:-}{\mathrm{LCState}}{≔}{\mathrm{irem}}{}\left({427419669081}{*}{\mathrm{LinearCongruence}}{:-}{\mathrm{LCState}}{,}{999999999989}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{irem}}{}\left({\mathrm{LinearCongruence}}{:-}{\mathrm{LCState}}{,}{6}\right){+}{1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2)
 > $M\left(\right)$
 ${4}$ (3)
 > $\mathrm{seq}\left(M\left(\right),i=1..10\right)$
 ${3}{,}{4}{,}{6}{,}{5}{,}{3}{,}{6}{,}{3}{,}{2}{,}{2}{,}{2}$ (4)
 > $M≔\mathrm{NewGenerator}\left(\mathrm{range}={10}^{10}\right)$
 ${M}{≔}{\mathbf{proc}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{LinearCongruence}}{:-}{\mathrm{LCState}}{≔}{\mathrm{irem}}{}\left({427419669081}{*}{\mathrm{LinearCongruence}}{:-}{\mathrm{LCState}}{,}{999999999989}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{irem}}{}\left({\mathrm{LinearCongruence}}{:-}{\mathrm{LCState}}{,}{10000000000}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (5)
 > $M\left(\right)$
 ${75487163}$ (6)
 > $\mathrm{seq}\left(M\left(\right),i=1..5\right)$
 ${7179490457}{,}{9169594160}{,}{8430571674}{,}{498834085}{,}{2920457916}$ (7)
 > $\mathrm{Float}\left(M\left(\right),-10\right)$
 ${0.3747019461}$ (8)
 > $\mathrm{seq}\left(\mathrm{Float}\left(M\left(\right),-10\right),i=1..5\right)$
 ${0.4031395307}{,}{0.0624947349}{,}{0.1053530086}{,}{0.6486307198}{,}{0.5590763466}$ (9)
 > $M≔\mathrm{NewGenerator}\left(\mathrm{range}={10}^{32}\right)$
 ${M}{≔}{\mathbf{proc}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{t}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{LinearCongruence}}{:-}{\mathrm{LCState}}{≔}{\mathrm{irem}}{}\left({427419669081}{*}{\mathrm{LinearCongruence}}{:-}{\mathrm{LCState}}{,}{999999999989}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{t}{≔}{\mathrm{LinearCongruence}}{:-}{\mathrm{LCState}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{to}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{2}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{LinearCongruence}}{:-}{\mathrm{LCState}}{≔}{\mathrm{irem}}{}\left({427419669081}{*}{\mathrm{LinearCongruence}}{:-}{\mathrm{LCState}}{,}{999999999989}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{t}{≔}{1000000000000}{*}{t}{+}{\mathrm{LinearCongruence}}{:-}{\mathrm{LCState}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end do}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{irem}}{}\left({t}{,}{100000000000000000000000000000000}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (10)
 > $M\left(\right)$
 ${92673709525428510973272600608981}$ (11)