File tree 3 files changed +24
-2
lines changed
3 files changed +24
-2
lines changed Original file line number Diff line number Diff line change @@ -172,8 +172,8 @@ void playlist_shuffle(struct playlist *pl)
172
172
pl -> entries [n ]-> original_index = n ;
173
173
mp_rand_state s = mp_rand_seed (0 );
174
174
for (int n = 0 ; n < pl -> num_entries - 1 ; n ++ ) {
175
- size_t j = ( size_t )(( pl -> num_entries - n ) * mp_rand_next_double ( & s ) );
176
- MPSWAP (struct playlist_entry * , pl -> entries [n ], pl -> entries [n + j ]);
175
+ size_t j = mp_rand_in_range32 ( & s , n , pl -> num_entries );
176
+ MPSWAP (struct playlist_entry * , pl -> entries [n ], pl -> entries [j ]);
177
177
}
178
178
playlist_update_indexes (pl , 0 , -1 );
179
179
}
Original file line number Diff line number Diff line change 22
22
23
23
#include <libavutil/random_seed.h>
24
24
25
+ #include "common/common.h"
26
+ #include "misc/mp_assert.h"
25
27
#include "osdep/timer.h"
26
28
#include "random.h"
27
29
@@ -84,3 +86,17 @@ double mp_rand_next_double(mp_rand_state *s)
84
86
{
85
87
return (mp_rand_next (s ) >> 11 ) * 0x1.0p-53 ;
86
88
}
89
+
90
+ // <https://web.archive.org/web/20250321082025/
91
+ // https://www.pcg-random.org/posts/bounded-rands.html#bitmask-with-rejection-unbiased-apples-method>
92
+ uint32_t mp_rand_in_range32 (mp_rand_state * s , uint32_t min , uint32_t max )
93
+ {
94
+ mp_assert (min < max );
95
+ uint32_t range = max - min ;
96
+ uint32_t mask = mp_round_next_power_of_2 (range ) - 1 ;
97
+ uint32_t ret ;
98
+ do {
99
+ ret = mp_rand_next (s ) & mask ;
100
+ } while (ret >= range );
101
+ return min + ret ;
102
+ }
Original file line number Diff line number Diff line change @@ -46,3 +46,9 @@ uint64_t mp_rand_next(mp_rand_state *s);
46
46
* distribution, and update the state accordingly.
47
47
*/
48
48
double mp_rand_next_double (mp_rand_state * s );
49
+
50
+ /*
51
+ * Return a 32 bit integer in the range [min, max) with uniform distribution.
52
+ * Caller should ensure `min < max`.
53
+ */
54
+ uint32_t mp_rand_in_range32 (mp_rand_state * s , uint32_t min , uint32_t max );
You can’t perform that action at this time.
0 commit comments