add rand_up_to
This was SVN commit r2170.
This commit is contained in:
parent
d1aa159215
commit
1f237be66b
@ -261,6 +261,15 @@ u16 fp_to_u16(double in)
|
||||
|
||||
|
||||
|
||||
// return random integer in [0, limit).
|
||||
// does not use poorly distributed lower bits of rand().
|
||||
int rand_up_to(int limit)
|
||||
{
|
||||
// (i64 avoids overflowing in multiply)
|
||||
const i64 ret = ((i64)limit * rand()) / (RAND_MAX+1);
|
||||
assert2(0 <= ret && ret < limit);
|
||||
return (int)ret;
|
||||
}
|
||||
|
||||
|
||||
// big endian!
|
||||
|
@ -311,6 +311,10 @@ extern uintptr_t round_up(uintptr_t val, uintptr_t multiple);
|
||||
|
||||
extern u16 fp_to_u16(double in);
|
||||
|
||||
// return random integer in [0, limit).
|
||||
// does not use poorly distributed lower bits of rand().
|
||||
extern int rand_up_to(int limit);
|
||||
|
||||
// big endian!
|
||||
extern void base32(const int len, const u8* in, u8* out);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user