How can i set the seed of random number generator ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How can i set the seed of random number generator ?

z_axis
This post was updated on .
In erlang, first i use the following function to set the seed:
new_seed() ->
    {_,_,X} = erlang:now(),
    {H,M,S} = time(),
    H1 = H * X rem 32767,
    M1 = M * X rem 32767,
    S1 = S * X rem 32767,
    put(random_seed, {H1,M1,S1}).

then use random:uniform/1 to get the random number.

In haskell, i just use the following function to get the random number. It seems i donot need to set the seed of random number generator manually?

rollDice ::  Int -> IO Int
rollDice n = randomRIO(1,n)

Sincerely!
e^(π.i) + 1 = 0
Reply | Threaded
Open this post in threaded view
|

Re: How can i set the seed random number generator ?

Scott Turner-8
On Monday 21 December 2009 20:37:30 zaxis wrote:

> In erlang, first i use the following function to set the seed:
> new_seed() ->
>     {_,_,X} = erlang:now(),
>     {H,M,S} = time(),
>     H1 = H * X rem 32767,
>     M1 = M * X rem 32767,
>     S1 = S * X rem 32767,
>     put(random_seed, {H1,M1,S1}).
>
> then use random:uniform/1 to get the random number.
>
> In haskell, i just use the following function to get the random number. It
> seems i donot need to set the seed of random number generator manually?
>
> rollDice ::  Int -> IO Int
> rollDice n = randomRIO(1,n)

That's correct.   randomRIO uses the global random number generator which is
automatically initialized with a different seed each time your program starts
up.
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: How can i set the seed random number generator ?

Chaddaï Fouché
On Tue, Dec 22, 2009 at 1:16 PM, Scott Turner <[hidden email]> wrote:
>> In haskell, i just use the following function to get the random number. It
>> seems i donot need to set the seed of random number generator manually?
>>
>> rollDice ::  Int -> IO Int
>> rollDice n = randomRIO(1,n)
>
> That's correct.   randomRIO uses the global random number generator which is
> automatically initialized with a different seed each time your program starts
> up.

but you can always change it with setStdGen, and you can always use
the non-IO part of System.Random (or even another random library
altogether, particularly recommended if you need performances as
System.Random is more than slow).

--
Jedaï
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe