Special simple random number generator

Posted by psihodelia on Stack Overflow See other posts from Stack Overflow or by psihodelia
Published on 2010-06-17T14:48:46Z Indexed on 2010/06/17 14:53 UTC
Read the original article Hit count: 142

Filed under:
|
|
|
|

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 staff is allowed.

© Stack Overflow or respective owner

Related posts about c++

Related posts about c