e r l a n g : c o o k b o o k

Erlang.NumberDiffRandNum
  Difference Topic NumberDiffRandNum (r1.1 - 24 Aug 2004 - BrentAFulgham)
Added:
>
>

%META:TOPICINFO{author="BrentAFulgham" date="1093388972" format="1.0" version="1.1"}%

Generating Different Random Numbers

Problem

You notice that several processes are getting the same "random" number sequence. You would like each process to be receiving its own unique random numbers.

Solution

Use the random:seed function to seed the pseudo-random number generator with a new seed value.

1> random:seed().    % Default seed from process dictionary
{5815,21695,596}
2> random:seed(5991,29821,991).
{3172,9814,20125}

Discussion

The pseudo-random number generator used in the Erlang implementation defaults to a constant value in the current process dictionary. Consequently, processes that happen to start at the same time could potentially seed with the same value. Using the random:seed function allows you to modify this default behavior.

Seeding a pseudo-random number generator sets its internal state deterministically. Seeding a generator with a particular number forces it to produce a sequence of pseudo-random numbers that is the same across runs and across platforms. This could be useful if you wish to provide the illusion of randomness.

See Also

-- BrentAFulgham - 24 Aug 2004 %META:FORM{name="CookbookForm"}% %META:FIELD{name="TopicType" title="TopicType" value="Recipe"}% %META:FIELD{name="ParentTopic" title="ParentTopic" value="NumberRecipes"}% %META:FIELD{name="TopicOrder" title="TopicOrder" value="090"}%

 
 
Copyright © 2004 by the contributing authors. All material on the Erlang Cookbook web site is the property of the contributing authors.
This material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest