Re: [eclipse-users] random - problem

From: Kish Shen <kisshen_at_...5...>
Date: Sun, 18 Nov 2007 01:01:43 +0000
Hi Marius,

Marius Schneider wrote:
> Hi!
> I am new in this mailing list and new with ECLiPSe.
>
> But I have a problem and I hope somebody can help me.
>
> When I use the random predicate of prolog in a java thread and the java
> thread is started, then I get always the same random-number. (e.g.
> eclipse.rpc("random(N)").arg(1);).
>
> How can I get "real" random numbers?
>
>   
As the bip documentation for random/1 says:

 >random/1 unifies N with a random integer between 0 and 2^31-1
 >(returned by the C library function random(), whose initialization
 >has been made using the pid of the running ECLiPSe ).

As I understand it, Java threads are run within the same process, so 
each thread will have the same pid (process id). Therefore you will have 
the same pseudo random sequence with each thread you start within the 
same Java program.

One way to get around this problem is use seed/1 with a different seed 
each time you start your embedded ECLiPSe, so that the random sequence 
will be different. The common way to do this is to use the current 
(unix) time as the seed, which will be different as long as your threads 
are not started within the same second.
In ECLiPSe, you can do this by:

 get_flag(unix_time, X), seed(X).

your subsequent calls to random/1 should be different, as long as your 
threads are not started within the same unix_time.

Cheers,

Kish Shen
Received on Sun Nov 18 2007 - 01:02:00 CET

This archive was generated by hypermail 2.2.0 : Mon Jul 09 2018 - 02:05:29 CEST