1
0
forked from 0ad/0ad

add rand_up_to

This was SVN commit r2170.
This commit is contained in:
janwas 2005-04-26 16:47:48 +00:00
parent d1aa159215
commit 1f237be66b
2 changed files with 13 additions and 0 deletions

View File

@ -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!

View File

@ -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);