19962013 Ericsson AB. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. random Joe Armstrong Bjarne Dacker 1 Bjarne Däcker 96-09-09 A random.sgml
random Pseudo random number generation

Random number generator. The method is attributed to B.A. Wichmann and I.D.Hill, in 'An efficient and portable pseudo-random number generator', Journal of Applied Statistics. AS183. 1982. Also Byte March 1987.

The current algorithm is a modification of the version attributed to Richard A O'Keefe in the standard Prolog library.

Every time a random number is requested, a state is used to calculate it, and a new state produced. The state can either be implicit (kept in the process dictionary) or be an explicit argument and return value. In this implementation, the state (the type ran()) consists of a tuple of three integers.

It should be noted that this random number generator is not cryptographically strong. If a strong cryptographic random number generator is needed for example crypto:rand_bytes/1 could be used instead.

The new and improved rand module should be used instead of this module.

The state.

Seeds random number generation with default values

Seeds random number generation with default (fixed) values in the process dictionary, and returns the old state.

Seeds random number generator

Seeds random number generation with integer values in the process dictionary, and returns the old state.

One easy way of obtaining a unique value to seed with is to:

random:seed(erlang:phash2([node()]), erlang:monotonic_time(), erlang:unique_integer())
Seeds random number generator

seed({A1, A2, A3}) is equivalent to seed(A1, A2, A3).

Return default state for random number generation

Returns the default state.

Return a random float

Returns a random float uniformly distributed between 0.0 and 1.0, updating the state in the process dictionary.

Return a random integer

Given an integer N >= 1, uniform/1 returns a random integer uniformly distributed between 1 and N, updating the state in the process dictionary.

Return a random float

Given a state, uniform_s/1returns a random float uniformly distributed between 0.0 and 1.0, and a new state.

Return a random integer

Given an integer N >= 1 and a state, uniform_s/2 returns a random integer uniformly distributed between 1 and N, and a new state.

Note

Some of the functions use the process dictionary variable random_seed to remember the current seed.

If a process calls uniform/0 or uniform/1 without setting a seed first, seed/0 is called automatically.

The implementation changed in R15. Upgrading to R15 will break applications that expect a specific output for a given seed. The output is still deterministic number series, but different compared to releases older than R15. The seed {0,0,0} will for example no longer produce a flawed series of only zeros.