MCPcopy Create free account
hub / github.com/assaultcube/AC / lrand

Function lrand

source/src/bot/bot_util.cpp:28–63  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

26long lseed; // our random number generator's seed
27
28long lrand (void)
29{
30 // this function is the equivalent of the rand() standard C library function,
31 // except that whereas rand() works only with short integers
32 // (i.e. not above 32767), this function is able to generate 32-bit random
33 // numbers. Isn't that nice ?
34 // credits go to Ray Gardner for his fast implementation of minimal random
35 // number generators
36 // http://c.snippets.org/snip_lister.php?fname=rg_rand.c
37
38 // compose the two 16-bit parts of the long integer and assemble them
39 static unsigned long lrand_lo, lrand_hi;
40 lrand_lo = 16807 * (long) (lseed & 0xFFFF); // low part
41 lrand_hi = 16807 * (long) ((unsigned long) lseed >> 16);
42 // high part
43 lrand_lo += (lrand_hi & 0x7FFF) << 16;
44 // assemble both in lrand_lo
45 // is the resulting number greater than LRAND_MAX (half the capacity) ?
46 if (lrand_lo > LRAND_MAX)
47 {
48 lrand_lo &= LRAND_MAX; // then get rid of the disturbing bit
49 lrand_lo++; // and increase it a bit (to avoid overflow problems, I suppose)
50 }
51
52 lrand_lo += lrand_hi >> 15;
53 // now do twisted maths to generate the next seed
54 // is the resulting number greater than LRAND_MAX (half the capacity) ?
55 if (lrand_lo > LRAND_MAX)
56 {
57 lrand_lo &= LRAND_MAX; // then get rid of the disturbing bit
58 lrand_lo++; // and increase it a bit (to avoid overflow problems, I suppose)
59 }
60 // now we've got our (pseudo-)random number.
61 lseed = (long) lrand_lo; // put it in the seed for next time
62 return (lseed); // and return it. Yeah, simple as that.
63}
64
65void lsrand (unsigned long initial_seed)
66{

Callers 3

lsrandFunction · 0.85
RandomLongFunction · 0.85
RandomFloatFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected