Branch: Tag:

2015-09-14

2015-09-14 17:25:21 by Martin Nilsson <nilsson@fastmail.com>

We only need 31 bits of random for a float, if we perform calculations at a higher precision.

2084:    *! @[random_seed()]    */    - /*! @decl float random(float max) -  *! -  *! This function returns a random number in the range @expr{0 .. @[max]-ɛ@}. -  *! -  *! @seealso -  *! @[random_seed()] -  */ -  +    PMOD_EXPORT   PIKEFUN int random(int i)   {
2103:    RETURN my_rand() % i;   }    + /*! @decl float random(float max) +  *! +  *! This function returns a random number in the range @expr{0 .. @[max]-ɛ@}. +  *! +  *! @seealso +  *! @[random_seed()] +  */ +    PMOD_EXPORT   PIKEFUN float random(float f)   {    if(f<=0.0) RETURN 0.0; - #define N 1048576 -  RETURN f * (my_rand()%N/((float)N)) + -  f * (my_rand()%N/( ((float)N) * ((float)N) )); +     -  + #define MASK ((1L<<32)-1) +  RETURN (float)(f * (((my_rand()&MASK)-1) / (double)MASK));   }      /*! @decl mixed random(array|multiset x)