[erlang-questions] more random uniform

Tony Rogvall <>
Sun Oct 27 18:14:27 CET 2013


No it was not necessary, just making sure it was fresh every time.

Any particular reason to keep the third argument to zero in the random:seed/3 call ?
This will, as you point out, generate an initial sequence of small random:uniform numbers.
Looking at random.erl the calculation is (modified in this context):

	(((X+1)*171) rem 30269) / 30269  +
	(((Y+1)*172) rem 30307) / 30307 + 
	(((0+1)*170) rem 30323) / 30323

Keeping the X and Y small enough (less than 176 ) we can rewrite this as:

	R = ((X+1)*171) / 30269 + ((Y+1)*172) / 30307 + 170 / 30323

The value of this expression will be small given small values on X and Y,  and the expression
trunc(R*3) will be zero for X+Y < 56 (roughly)

In other words use larger numbers, and use them all, when seeding the random number generator.

/Tony


On 27 okt 2013, at 11:48, Bengt Kleberg <> wrote:

> 7> [erlang:spawn( fun() -> random:seed(X,Y,0), io:fwrite("~p ", [random:uniform(3)]) end ) || X<-lists:seq(1,10), Y<-lists:seq(1,10)]. 
> 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
> 
> 
> Sent from Moxier Mail
> (http://www.moxier.com)
> 
> 
> ----- Ursprungligt meddelande -----
> Från: Bengt Kleberg <>
> Till: null
> Kopia: "" <>
> Skickat: 27-10-2013 11:33 fm
> Ämne: Re: [erlang-questions] more random uniform
> 
> 
> Thank you.
> 
> I have 15B.
> Is the erase/1 necessary? I am using new processes each time.
> 
> 
> Bengt
> 
> Sent from Moxier Mail
> (http://www.moxier.com)
> 
> 
> ----- Ursprungligt meddelande -----
> Från: Tony Rogvall <>
> Till: Bengt Kleberg <>
> Kopia: "" <>
> Skickat: 27-10-2013 11:13 fm
> Ämne: Re: [erlang-questions] more random uniform
> 
> 
> I can not replicate this (on R16B01).
> 
> > erase(random_seed), random:seed(erlang:now()), random:uniform(3).
> 1
> > erase(random_seed), random:seed(erlang:now()), random:uniform(3).
> 2
> ...
> 
> ?
> 
> /Tony
> 
> 
> On 27 okt 2013, at 10:45, Bengt Kleberg <> wrote:
> 
>> Greetings,
>> 
>> When doing random:uniform/1 for small values, ie 3, the result is 1 the first time. Changing the seed does not help.
>> What am I missing?
>> 
>> 
>> Bengt
>> 
>> Sent from Moxier Mail
>> (http://www.moxier.com)
>> _______________________________________________
>> erlang-questions mailing list
>> 
>> http://erlang.org/mailman/listinfo/erlang-questions
> 
> "Installing applications can lead to corruption over time. Applications gradually write over each other's libraries, partial upgrades occur, user and system errors happen, and minute changes may be unnoticeable and difficult to fix"
> 
> 
> 
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions

"Installing applications can lead to corruption over time. Applications gradually write over each other's libraries, partial upgrades occur, user and system errors happen, and minute changes may be unnoticeable and difficult to fix"



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20131027/0a80e923/attachment.html>


More information about the erlang-questions mailing list