1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
/**********************************************************************
int wn_random_mod_int(mod)
**********************************************************************/
/****************************************************************************
COPYRIGHT NOTICE:
The source code in this file is provided free of charge
to the author's consulting clients. It is in the
public domain and therefore may be used by anybody for
any purpose.
AUTHOR:
Will Naylor
****************************************************************************/
#include <stdio.h>
#include <string.h>
#include "wnlib.h"
#include "wnrnd.h"
local int get_num_bits(register long unsigned int mod)
{
register int ret;
--mod;
for(ret=0;ret<32;++ret)
{
if(mod == 0)
{
break;
}
mod >>= 1;
}
return(ret);
}
long unsigned int wn_random_mod_int(long unsigned int mod)
{
int num_bits;
long unsigned int ret;
if(mod < 2)
{
return(0);
}
num_bits = get_num_bits(mod);
do
{
ret = wn_random_n_bits(num_bits);
}
while(ret >= mod);
return(ret);
}
|