HELP: random$Integer

40 views
Skip to first unread message

Ralf Hemmecke

unread,
Feb 23, 2019, 9:08:51 AM2/23/19
to fricas-devel
Hello,

I use random(n)$Integer in a program and use that to generate a
filename. Unfortunately, when I run that program in parallel, i.e., like
two instances of

cat prog.input | fricas -nosman

it produces exactly the same random integer.

Probably understandable, however I haven't found a way to give different
seeds the random number generator.

Any suggestion?

In fact, I don't actually care about random numbers if only I get a
unique filename to which I can add different extensions.

Ralf

oldk1331

unread,
Feb 23, 2019, 10:06:51 AM2/23/19
to fricas...@googlegroups.com
"random" in Integer simply calls "random" in common lisp.

And I looked at common lisp standard (section 12.9, CLTL2),
the standard way to have a new seed is

)lisp (setf *random-state* (make-random-state t))

but the detail is implementation dependent.

That makes me to think we should have a standard random
number generator in FriCAS that is independent on underlying Lisp.
> --
> You received this message because you are subscribed to the Google Groups "FriCAS - computer algebra system" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to fricas-devel...@googlegroups.com.
> To post to this group, send email to fricas...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/fricas-devel/9b2814a7-ee25-a99d-6105-c59d539b3f37%40hemmecke.org.
> For more options, visit https://groups.google.com/d/optout.

Ralf Hemmecke

unread,
Feb 23, 2019, 12:19:01 PM2/23/19
to fricas...@googlegroups.com
Hi Qian,

than you for your lisp statement.

> That makes me to think we should have a standard random
> number generator in FriCAS that is independent on underlying Lisp.

Hmmm... right. But we have.

http://fricas.github.io/api/RandomNumberSource

Unfortunately, there is no function that generates a truely random seed
from the time or /dev/[u]random.

What about adding a new runtime support function like your

(setf *random-state* (make-random-state t))

and make that available in RandomNumberState.

I don't know whether it would be wise to replace random$Integer that is
implemented by just calling RANDOM$Lisp by reference to RandomNumberState.

Anyway, it would be nice to have add function that initializes the seed
by some truely random value. Currently I see no source to get such a
random value (except calling Lisp directly -- which I want to avoid in
my code).

Ralf

Waldek Hebisch

unread,
Feb 23, 2019, 12:36:27 PM2/23/19
to fricas...@googlegroups.com
Ralf Hemmecke
>
> Anyway, it would be nice to have add function that initializes the seed
> by some truely random value. Currently I see no source to get such a
> random value (except calling Lisp directly -- which I want to avoid in
> my code).

Well, trurly random value is hard to get, on Linux resonably good
approximation is '/dev/uradom' ('/dev/random' may be better, but
it blocks when it has too little randomness). OTOH trurly
random values have little use outside cryptography. For
most uses psuedorandom number are good enough, and to ease
debugging we want them to be repeatable.

Of course, it would be good to have some way of getting
nonrepratable numbers. Old school used current time to
seed generator -- not very random, but nonrepratable
enough for many purposes. Anyway, lack of such functions
is partially oversight, partially because doing it right
is hard.

BTW. For somewhat unique file name it is usual to use
process id -- in Unix each process have unique id so
no chance of clash with another process running simultaneousy
on the same machine. There is also special C library
function for generating temporary files: it tries somewhat
random name and creates file in exclusive mode. If somebody
already has this file the utility loops. The point here is
that with some amount of randomenss chance of clash is
reasonably low and the way of opening means that file
is unique. That would need Posix bindings -- I am
thining of adding that (bypassing Lisp), but other
things keep me busy.

--
Waldek Hebisch

Ralf Hemmecke

unread,
Feb 23, 2019, 6:09:54 PM2/23/19
to fricas...@googlegroups.com
> Well, trurly random value is hard to get, on Linux resonably good
> approximation is '/dev/uradom' ('/dev/random' may be better, but it
> blocks when it has too little randomness).

Oh, /dev/urandom would be sufficient for my purpose. I'm not doing
cryptography.

Pseudorandom numbers are otherwise also quite enough. But it should be
possible to seed them in a somewhat random way.

Since I have no idea how to read a SingleInteger from /dev/urandom, I
must wait until you find time to add it to FriCAS.

But it is not too urgent.

While looking for another solution to my problem, I noticed that
Matrix(X) has no hashUpdate! implemented.

> BTW. For somewhat unique file name it is usual to use process id

Yes, but how to get the process id in SPAD?

x: SingleInteger := processID() ???

> If somebody already has this file the utility loops. The point here
> is that with some amount of randomenss chance of clash is reasonably
> low and the way of opening means that file is unique. That would
> need Posix bindings -- I am thining of adding that (bypassing Lisp),
> but other things keep me busy.

No, I don't just want to create a file with random name. I need a
filename that is not in use. Together with a file random.mat I need to
create a file random.rhs with the same name but different extension.

So in some sense /dev/urandom would be the better solution to my problem
at hand.

Ralf

Ralf Hemmecke

unread,
Feb 23, 2019, 6:15:03 PM2/23/19
to fricas...@googlegroups.com
Here is the corresponding patch for the missing hashUpdate!.
Can I commit?

Ralf
0001-add-hashUpdate-to-InnerIndexedTwoDimensionalArray.patch

Waldek Hebisch

unread,
Feb 25, 2019, 12:07:38 PM2/25/19
to fricas...@googlegroups.com

>
> Here is the corresponding patch for the missing hashUpdate!.
> Can I commit?
>
> Ralf
>

Yes, please do.

--
Waldek Hebisch

Ralf Hemmecke

unread,
Feb 18, 2021, 8:46:40 AM2/18/21
to FriCAS - computer algebra system
Hello,

this continues a thread
that I started in 2019 and is connected to this thread about random numbers.

I could now use Qian's suggestion

)lisp (setf *random-state* (make-random-state t))

since in my case I can simply pipe that command in with every new start
of FriCAS (like in FRICAS_INIT).

However, since random$Integer is, in fact, just calling Lisp, it would be nice to
add a function

seedRandom(): Void == SEED_-RANDOM()$Lisp

to integer.spad and si.spad or simply into the category IntegerNumberSystem

with an implementation

(defun seed-random () (setf *random-state* (make-random-state t)))

in vmlisp.lisp or somewhere else.

As far as I understand the documentation of (make-random-state t), it gets its
randomness from/dev/urandom or time+pid if possible. Both would be good enough
for my case and even if it does not help on some weird systems, it should certainly
be a bit better than having no random source at all.

What's your opinion?
Waldek would you include such code? If yes, where?

Ralf

Waldek Hebisch

unread,
Feb 18, 2021, 1:01:32 PM2/18/21
to fricas...@googlegroups.com
We can include it. I would call Lisp function SEED_RANDOM, so
that exactly the same sequence of characters is used as Spad
and Lisp name. I think that proper place for it is in 'primitives.lisp'
('vmlisp.lisp' is for legacy things).

--
Waldek Hebisch
Reply all
Reply to author
Forward
0 new messages