[mercury-users] Converting time_t to int using common library?
Julian Fondren
ayrnieu at gmail.com
Tue Feb 13 19:58:36 AEDT 2007
On 2/13/07, doug.auclair at logicaltypes.com <doug.auclair at logicaltypes.com> wrote:
> So, is there an elegant way to convert time_t to int
> using the supplied modules in the library that I'm
> missing? If not, could module random be modified
> to allow a seed to initialize the supply to be a time_t
> as well as an int?
As an alternate solution, attached is a a patch that
adds to library/random.m ; see docs, below.
Cheers,
Julian
% random.init(RS, !IO): creates a supply of random numbers RS
% using a sensible default seed (e.g., on POSIX it will use
% the number of seconds since the unix epoch.)
%
:- pred random.init(random.supply::uo, io::di, io::uo) is det.
% random.init(RS): creates a supply of random numbers RS
% using a sensible default seed (e.g., on POSIX it will use
% the number of seconds since the unix epoch.) This function
% differs from the previous predicate in that it only determines
% the 'sensible default seed' when first evaluated, and
% thereafter uses that same seed.
%
:- func random.init = (random.supply::uo) is det.
-------------- next part --------------
*** mercury-compiler-0.13.1/library/random.m Mon Jul 31 15:01:56 2006
--- random.m Tue Feb 13 05:31:37 2007
***************
*** 53,59 ****
:- import_module list.
% The type `random.supply' represents a supply of random numbers.
! %
:- type random.supply.
--- 53,60 ----
:- import_module list.
+ :- import_module io.
% The type `random.supply' represents a supply of random numbers.
! %
:- type random.supply.
***************
*** 63,69 ****
--- 64,86 ----
:- pred random.init(int::in, random.supply::uo) is det.
+ % random.init(RS, !IO): creates a supply of random numbers RS
+ % using a sensible default seed (e.g., on POSIX it will use
+ % the number of seconds since the unix epoch.)
+ %
+ :- pred random.init(random.supply::uo, io::di, io::uo) is det.
+
+ % random.init(RS): creates a supply of random numbers RS
+ % using a sensible default seed (e.g., on POSIX it will use
+ % the number of seconds since the unix epoch.) This function
+ % differs from the previous predicate in that it only determines
+ % the 'sensible default seed' when first evaluated, and
+ % thereafter uses that same seed.
+ %
+ :- func random.init = (random.supply::uo) is det.
+
% random.random(Num, RS0, RS): extracts a number Num in the
% range 0 .. RandMax from the random number supply RS0, and
% binds RS to the new state of the random number supply.
+ %
:- pred random.random(int, random.supply, random.supply).
:- mode random.random(out, mdi, muo) is det.
***************
*** 134,137 ****
--- 151,177 ----
random.init(I0, rs(RS)) :-
copy(I0, RS).
+
+ :- pragma promise_pure(random.init/3).
+ random.init(rs(RS), !IO) :-
+ semipure random.generate_seed(Seed),
+ copy(Seed, RS).
+
+ :- pragma promise_pure(random.init/0).
+ :- mutable(generated_seed, int, 0, ground, [untrailed]).
+ random.init = rs(RS) :-
+ semipure random.get_generated_seed(Seed),
+ ( Seed = 0 ->
+ semipure random.generate_seed(NewSeed),
+ impure random.set_generated_seed(NewSeed),
+ copy(NewSeed, RS)
+ ;
+ copy(Seed, RS)
+ ).
+
+ :- pragma c_header_code("#include <time.h>").
+ :- semipure pred random.generate_seed(int::out) is det.
+ :- pragma c_code(random.generate_seed(Time::out),
+ [will_not_call_mercury, thread_safe, promise_semipure],
+ "Time = time(NULL);").
random.random(I, rs(RS0), rs(RS)) :-
More information about the users
mailing list