diff options
author | Adam Borowski <kilobyte@angband.pl> | 2013-09-10 09:16:46 +0200 |
---|---|---|
committer | Adam Borowski <kilobyte@angband.pl> | 2013-09-10 13:35:07 +0200 |
commit | 30670662abe8d1661a589e493ca44220dc92b4d9 (patch) | |
tree | 7ed038b1d931862efd09caf6632751b7e038d8b7 /crawl-ref/source/random.h | |
parent | cb2faac032b05e6105bba624180e36aa3202a314 (diff) | |
download | crawl-ref-30670662abe8d1661a589e493ca44220dc92b4d9.tar.gz crawl-ref-30670662abe8d1661a589e493ca44220dc92b4d9.zip |
Get rid of random_shuffle().
This should hopefully make games from the same seed proceed the same on all
architectures[1], as I can't think of anything else that behaves differently
based on pointer size, endianness or exact pointer values.
I used a NIH implementation instead of passing a third argument to
random_shuffle, as the interface is so much nicer.
[1]. This will be affected by terminal size (elemental colours are not
resolved outside the screen), tile windows size (random animations),
tiles/non-tiles and possibly others.
Also, I don't think we use STL hashes anywhere, but if we'd do, the STL
implementation will matter. Please don't make this stop you: this commit
is only to make test cases from my stress tests portable which is a small
benefit, perhaps even smaller than the nicer call interface.
Diffstat (limited to 'crawl-ref/source/random.h')
-rw-r--r-- | crawl-ref/source/random.h | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/crawl-ref/source/random.h b/crawl-ref/source/random.h index 374b89b8f3..476a87b6f6 100644 --- a/crawl-ref/source/random.h +++ b/crawl-ref/source/random.h @@ -99,6 +99,25 @@ struct dice_def dice_def calc_dice(int num_dice, int max_damage); void scale_dice(dice_def &dice, int threshold = 24); +template <typename T> +void shuffle_array(T* arr, int n) +{ + while (n > 1) + { + int i = random2(n); + n--; + T tmp = arr[i]; + arr[i] = arr[n]; + arr[n] = tmp; + } +} + +template <typename T> +void shuffle_array(vector<T> &vec) +{ + shuffle_array(&vec[0], vec.size()); +} + class rng_save_excursion { public: |