views:

322

answers:

5

How to create a function, which on every call generates a random integer number? This number must be most random as possible (according to uniform distribution). It is only allowed to use one static variable and at most 3 elementary steps, where each step consists of only one basic arithmetic operation of arity 1 or 2.

Example:

int myrandom(void){
  static int x;
  x = some_step1;
  x = some_step2;
  x = some_step3;
  return x;
}

Basic arithmetic operations are +,-,%,and, not, xor, or, left shift, right shift, multiplication and division. Of course, no rand(), random() or similar stuff is allowed.

+13  A: 

the Linear Congruential Generator is one of the oldest and simplest method:

int seed = 123456789;

int rand()
{
  seed = (a * seed + c) % m;
  return seed;
}

Ony few instruction with basic arithmetic operations, that's what you needed.

Mind that this algorithm works fine just if a, c and m are chosen in a particular way!

To guarantee the longest possible period of this sequence c and m should be coprime, a-1 should be divisible by all prime factors of m and also for 4 if m is divisible by 4.

Some examples of values are shown in Wikipedia: for example ANSI C for some compilers proposes m = 2^32, a = 1103515245 and c = 12345

Jack
Worth noting that `m = 2^32` won’t work in your above code … for such a value, the `% m` operation can just be removed (and that’s indeed the reason for choosing it).
Konrad Rudolph
There is no seed given.
psihodelia
@psihodelia - what is 'int seed = 123456789;' then?
KevinDTimm
He means that it's not supposed to have a seed. @psihodelia 'seed' is the static variable you are allowed. It has to have an initial value of some sort.
Nick Johnson
Nick, the solution moved seed from inside the function call to outside the function call (after renaming it from 'x'). psi can claim there is no seed, but, by definition, 'static int x' is always going to be valued at 0, therefore being no different from 'int seed = 123456789'
KevinDTimm
one of the oldest, simplest and definitely badest.
Alexandre C.
+1  A: 

You might have a look at this. It's far from beeing a "perfect" random number generator, but it does fulfil your requirements as far as i can see.

Here you can find some additional information about random number generation.

phimuemue
A: 

Boost has a very nice random number library, and the source code is available, so you could try looking there and using what you need (i.e. cut and paste).

andand
+3  A: 

Here's a function with uniform distribution over the entire range of int:

int rand()
{
  static int random = 0;
  return random++;
}
Bill
Not entirely random, however. =P Although I suppose it's exactly as random as any other psuedorandom generator.
Erik Forbes
not random: it is easily recognizable how it is produced (the rule of production), so this qualifies it as not random (and very bad pseudorandom).
ShinTakezou
it produces all outputs with an equal probability = random
Martin Beckett
@Shin: I never claimed it was a *good* PRNG, just that it was a PRNG. :P
Bill
the OPer was asking for a good PRNG, or at least for a not so bad PRNG;; @Maring Beckett, it is simply false, it is not random: http://en.wikipedia.org/wiki/Randomness#Randomness_measures_and_tests i.e., even though it follows the statistics, that sequence can't be generated by not-related stochasting "events", so it can be demonstrated that it is not random (for all the fields where randomness is required; of course you can insist that this rand() generates random numbers, but noone will use that "random" generator if s/he needs randomness)
ShinTakezou
@Shin: The OP was asking for a solution to an interview question, and the only requirement I saw was a uniform distribution. :)
Bill
s/he says "This number must be most random as possible (according to uniform distribution)". Even though it is not so precisely expressed, it is obviously not considerable a solution like yours, since the "simple-worded" requirement "most random as possible" rules out your generator (that as explained already, can't be used to have a source of randomness)
ShinTakezou
There is absolutely no reason why that sequence couldn't be generated by independent random events. It is *vanishingly* unlikely, but so is *any* particular sequence, a priori.
caf
@Shin: How are you planning to prove that any particular solution is the "most random possible" solution?
Bill
@Bill I don't need to prove it. You instead have to prove that 1 2...n-1 n n+1... is a random seq,and that can be considered a "real" good random seq;and (@caf)it'd change the theory if it'd be produced by independent stochastic events:we should conclude they are not indenpendent.There's a reason why even the simpler PRG does not work that way and try to generate a sequence that looks random (even though further analysis would prove it is not,and this is about doing good PRGs:making harder to recognize its pseudorandomness,longer the seq needed to recognize it)
ShinTakezou
Moreover(@caf)your reasoning is about the "classical" simplistic vision of many statistics-acquainted persons,and I've already treated indirectly this before your comments,so you have not deepened the _complex_ argument of randomness.Not saying wikipedia has the full answer,but it is a starting point easy to be shared (instead of lifting my back going to search books where I first have seen the argument treated)and that already has most of the needed "pointers", keywords and clues http://en.wikipedia.org/wiki/Randomness
ShinTakezou
A: 

If I write man rand, I can read a possible example, given in POSIX.1-2001, for implementing rand() and srand(). See e.g. here. If you need something more sophisticated, take a look at GNU Scientific Library; you can of course download the code and see the implementation(s).

ShinTakezou