How to initialize seed for library(random)?

Help requests on developing Logtalk applications

Moderator: Paulo Moura

Post Reply
dram
Posts: 13
Joined: Thu Feb 16, 2017 9:28 am

How to initialize seed for library(random)?

Post by dram » Sat Feb 25, 2017 8:09 am

After reading through documents of library(random), I failed to find a way to properly initialize the random seed.

Currently, I use following code after peeking into the source of random.lgt, not sure if it is correct:

Code: Select all

time::now(H, M, S),
random::set_seed(seed(H, M, S))
But argument of `set_seed` is said to be opaque, so this way is a hack.

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: How to initialize seed for library(random)?

Post by Paulo Moura » Sat Feb 25, 2017 11:00 am

The current random library is a portable pseudo-random number generator. The idea of the get_seed/1 and set_seed/1, is to allow reproducibility as explained in the library documentation:

http://logtalk.org/library/randomp_0.html

Considering that we want to keep the library portable, a solution for what it seems you want to accomplish would be to add a public randomize/1 predicate that would take an integer. Would that work for you?
Paulo Moura
Logtalk developer

dram
Posts: 13
Joined: Thu Feb 16, 2017 9:28 am

Re: How to initialize seed for library(random)?

Post by dram » Sat Feb 25, 2017 11:53 am

Yes, that will work for me.

Following are some random thoughts:

1. If we add `randomize/1`, a predicate to get current time in seconds is also needed, AFAIK currently there is no such thing in library(time).

2, Or we can add something like `randomize/0` in GProlog[1], which use time as seed internally, but it can not be used to make reproducible random sequences.

Personally, I prefer the first one.

[1]: http://www.gprolog.org/manual/html_node ... tml#sec246

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: How to initialize seed for library(random)?

Post by Paulo Moura » Sat Feb 25, 2017 11:57 am

Committed some improvements to the random library:

https://github.com/LogtalkDotOrg/logtal ... 7df07a0e1b

Feedback welcome. Looking into the feasibility of also adding a randomize/0 predicate (portability is the possible issue here).
Paulo Moura
Logtalk developer

dram
Posts: 13
Joined: Thu Feb 16, 2017 9:28 am

Re: How to initialize seed for library(random)?

Post by dram » Sat Feb 25, 2017 12:30 pm

Wow! What a quick fix.

Personally, I do not quite like the name `frandom`, for following reasons:

1. I really appreciate the naming conversion in Logtalk code that abbreviations are used very sparsely, so I think `fast_random` is better, although not quite good (see below).

2. `frandom` may be confused with a Linux module[1] which has the same name.

3. "fast" is a bit vague, my first impression is that it uses a faster algorithm, and even tried to run `diff` between those two files to find the differences.


I can not thought out better names, maybe we can name "single random generator" as `random`, and "multiple random generators" as some thing else, but that will break compatibility.

[1] https://wiki.archlinux.org/index.php/frandom

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: How to initialize seed for library(random)?

Post by Paulo Moura » Sat Feb 25, 2017 12:55 pm

I'm currently accessing the performance gains of frandom over random. The speedup comes from avoiding use of dynamic binding to retrieve and save the random seed. But several predicates (e.g. sequence/4 are already optimized to avoid that penalty). Depending on the results, I may drop frandom altogether.
Paulo Moura
Logtalk developer

dram
Posts: 13
Joined: Thu Feb 16, 2017 9:28 am

Re: How to initialize seed for library(random)?

Post by dram » Sun Feb 26, 2017 1:51 am

Reasonable.

Now, with the newly added `randomize/1`, I use following code in SWI-Prolog:

Code: Select all

get_time(Time),
Seconds is truncate(Time),
random::randomize(Seconds)

dram
Posts: 13
Joined: Thu Feb 16, 2017 9:28 am

Re: How to initialize seed for library(random)?

Post by dram » Sun Feb 26, 2017 1:59 am

Just found that there is `os::time_stamp/1` in Logtalk, so code can be fully portable:

Code: Select all

os::time_stamp(Time),
Seconds is truncate(Time),
random::randomize(Seconds)

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: How to initialize seed for library(random)?

Post by Paulo Moura » Sun Feb 26, 2017 2:19 am

The os::time_stamp/1 predicate returns an opaque term that is comparable but this term is not a number for all supported backend Prolog systems (for the simple reason that no all systems provide the necessary functionality).

For a portable solution, I would suggest using instead the os::date_time/7 predicate and doing some math with the returned values to get an integer to pass to the random::randomize/1 predicate. I may use this or a similar solution to provide a random::randomize/0 predicate.
Paulo Moura
Logtalk developer

dram
Posts: 13
Joined: Thu Feb 16, 2017 9:28 am

Re: How to initialize seed for library(random)?

Post by dram » Sun Feb 26, 2017 2:53 am

Ah, I see, return type of os::time_stamp/1 has been changed in 3.09.3.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest