rng_uniform

The rng_uniform operator produces a pseudo random number sequence. Available only in the Enterprise Edition.

Synopsis

rng_uniform(existing_array | schema
[, min [, max [, generatorName [, seed]]]]);

Library

The rng_uniform operator resides in the Linear Algebra library. Run the following query to load this library:

AFL% load_library('linear_algebra'); 

Inputs 

The rng_uniform operator takes the following arguments:

  • existing_array:   An array that exists in the SciDb database.
  • schema:  Either an existing 1-dimensional array, with single attribute of type double, or an equivalent schema.
  • min (optional):  The lower value of the uniform range. The default is 0.0. It may appear in the output when max - min is less than 1.
  • max (optional):  The upper value of the uniform range. The default value is 1.0. It must be greater than min. It is guaranteed to not appear in the output.
  • generatorName (optional):  A string representing the algorithm used. Currently, the only available value is drand48, which uses a linear congruential algorithm and 48-bit integer arithmetic. The generator has a maximum period of 248 (repeats itself every 281.474 trillion values).
  • seed (optional):  An integer value used to initialize the pseudo-random number generator.  The default value is 13070. 

Summary

The rng_uniform operator produces a pseudo-random number sequence that is completely determined by: min, max, generatorName, and seed.  The output is a 1-dimensional array with values in the range (min, max].

Examples

Generate a Sequence of 10 pseudo-random numbers and Store them in an Array

To generate a sequence of 10 pseudo-random numbers, and store them into an array, do the following:

  1. First load the library:

    AFL% load_library('linear_algebra');
    

    The output is:

    Query was executed successfully


  2. Next create the array to store the random numbers

    AFL% create array A <val:double>[i=0:9]; 
    


    The output is:

    Query was executed successfully
  3. Now generate the random numbers and store them into the array:

    AFL% store(rng_uniform(A),A); 
    


    The output is:

    {i} val
    {0} 0.170828
    {1} 0.749902
    {2} 0.0963717
    {3} 0.870465
    {4} 0.577304
    {5} 0.785799
    {6} 0.692194
    {7} 0.368766
    {8} 0.873904
    {9} 0.745095

    Note that the example uses default settings for all of the optional parameters. 

  4. The following query is equivalent (all defaults explicitly specified):

    AFL% store(rng_uniform(A, 0.0, 1.0, 'drand48', 13070),A);
    


    The output is:

    {i} val
    {0} 0.170828
    {1} 0.749902
    {2} 0.0963717
    {3} 0.870465
    {4} 0.577304
    {5} 0.785799
    {6} 0.692194
    {7} 0.368766
    {8} 0.873904
    {9} 0.745095
  5. Now remove the array: 

    AFL% remove(A);
    


    The output is:

    Query was executed successfully

See how min, max, and seed effect the Generated Number Sequence

To use the same seed, and different ranges to show that the sequence of generated numbers depends on the min, max, and seed values only, do the following:

  1. Produce a sequence of pseudo-random numbers, in the range [0.0, 1.0):

    AFL% rng_uniform(<v:double> [i=0:9], 0.0, 1.0, 'drand48', 10000); 


    The output is:

    {i} rng_uniform
    {0} 0.895813
    {1} 0.67881
    {2} 0.597668
    {3} 0.23083
    {4} 0.390387
    {5} 0.243181
    {6} 0.291912
    {7} 0.880904
    {8} 0.420518
    {9} 0.404442
  2. Produce a similar sequence, but in the range of [0.0, 7.0):

    AFL% rng_uniform(<v:double> [i=0:9], 0.0, 7.0, 'drand48', 10000); 


    The output is:

    {i} rng_uniform
    {0} 6.27069
    {1} 4.75167
    {2} 4.18368
    {3} 1.61581
    {4} 2.73271
    {5} 1.70227
    {6} 2.04339
    {7} 6.16633
    {8} 2.94362
    {9} 2.8311

     

  3. Verify that the second sequence is equivalent to the first sequence, multiplied by 7:

    AFL% project
             (apply
                  (join (
                       rng_uniform(<v:double> [i=0:9], 0.0, 1.0, 'drand48', 10000) as A,
                       rng_uniform(<v:double> [i=0:9], 0.0, 7.0, 'drand48', 10000) as B),
                   seven_A, A.rng_uniform * 7),
          B.rng_uniform, seven_A
    ); 


    The output is:

    {i} rng_uniform,seven_A
    {0} 6.27069,6.27069
    {1} 4.75167,4.75167
    {2} 4.18368,4.18368
    {3} 1.61581,1.61581
    {4} 2.73271,2.73271
    {5} 1.70227,1.70227
    {6} 2.04339,2.04339
    {7} 6.16633,6.16633
    {8} 2.94362,2.94362
    {9} 2.8311,2.8311 Â