How do I force GAP give me a true random number?

250 Views Asked by At

I am using GAP (see https://www.gap-system.org/) in a number theory class to display a variety of number theoretic functions and number theory problems. I've written code in GAP to implement the RSA encryption scheme but unfortunately, the random number generation I am using (x := Random([1..10^18]); ) gives every student the same random number! The goal is for each student to generate two random primes of a certain length and use them in their RSA encryption. This is, of course, defeated if we all get the same "random" number!

How do I force GAP to choose something truly random, something that cannot be duplicated by another student?

2

There are 2 best solutions below

1
On BEST ANSWER

Expanding my comment to include an example. You can use really random sources provided by the IO package. It should be loaded by LoadPackage("io"); although in most cases it will be loaded by default, if available. Windows GAP distribution includes IO binaries, so that should not be a problem to run it on Windows. Linux and MacOS users should have it compiled.

Then try the following:

gap> r:=RandomSource(IsRealRandomSource,"random");
<a real random source>
gap> Random(r,[1..10^18]);
607661511822560412

Now, in a new GAP session I have

gap> r:=RandomSource(IsRealRandomSource,"random");
<a real random source>
gap> Random(r,[1..10^18]);
500068505640897

so it seems to work as you need.

On the other hand, Alexander's number fits the purpose, and does not require you to modify an existing code by adding an additional argument to calls to Random, so I suggest to go for it.

2
On

The default in GAP is that the random number generator always initializes with the same seed to make calculations reproducible.

One could use e.g. CurrentDateTimeString to re-seed the default random number generator with a changing initial value that is unlikely to be the same for two students, if they work unsynchronized.

Reset(GlobalMersenneTwister,CurrentDateTimeString());;

While this is probably not good enough for military grade cryptography, it should scramble sufficiently for classroom purposes.