diff options
author | Adam Borowski <kilobyte@angband.pl> | 2013-10-06 17:16:52 +0200 |
---|---|---|
committer | Adam Borowski <kilobyte@angband.pl> | 2013-10-06 17:18:39 +0200 |
commit | ba44991f064c3db2626e787dc95d0719877c89b0 (patch) | |
tree | 1d3aefaa14238ddbe0d4294e848b740af68607f0 /crawl-ref/source/random.cc | |
parent | c47ee9af642d5ad698871c7ce31e5ffae6a51d98 (diff) | |
download | crawl-ref-ba44991f064c3db2626e787dc95d0719877c89b0.tar.gz crawl-ref-ba44991f064c3db2626e787dc95d0719877c89b0.zip |
random3(), with a different RNG state.
Works same as random2().
Also, stops valgrind from kvetching about unfreed asg state.
Diffstat (limited to 'crawl-ref/source/random.cc')
-rw-r--r-- | crawl-ref/source/random.cc | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/crawl-ref/source/random.cc b/crawl-ref/source/random.cc index e4923ab79e..11be6a023a 100644 --- a/crawl-ref/source/random.cc +++ b/crawl-ref/source/random.cc @@ -109,6 +109,24 @@ int random2(int max) } } +// [0, max), separate RNG state +int random3(int max) +{ + if (max <= 1) + return 0; + + uint32_t partn = UINT32_MAX / max; + + while (true) + { + uint32_t bits = get_uint32(); + uint32_t val = bits / partn; + + if (val < (uint32_t)max) + return ((int)val); + } +} + // [0, 1] bool coinflip(void) { |