Jump to content

The inefficiency of random number spreads via modulo division


Recommended Posts

Posted this on MFG about a year ago, but realized I never posted it here, so here we go.

 

Writing this up out of boredom due to one of my own personal experiences in MUGEN coding...

Many times I've seen authors use modulo division to generate a spread of random numbers for whatever purpose (be it for AI triggers, random animations, or whatever). For those who don't know what I'm talking about, I'm referring to pieces of code like this:

trigger1 = random%2 = 0

To understand why the above code is inefficient, we have to take a deeper look into the results its producing.

For those who don't already know, in MUGEN, random generates a random integer from 0 to 999 (effectively producing 1 of 1000 different values each time random is called), each with an equal probability of being returned by random. With the % operator, such as in expression X%Y, we are dividing value X by value Y and returning only the remainder as a result. 17%10, for example, would return 7 as its value (17/10 = 1 R7). If we were to apply modulo division to random, for instance, we would be generating a random value from 0 to 999 and then dividing this number by an arbitrary value and returning its remainder as a result. By applying modulo division to random, we expect to see results that have an equal probability of being generated, as follows:

Random%10 generates:
0 (10% probability)
1 (10% probability)
2 (10% probability)
3 (10% probability)
4 (10% probability)
5 (10% probability)
6 (10% probability)
7 (10% probability)
8 (10% probability)
9 (10% probability)

As we can see above, we have a nice even spread of the 10 possible values that random%10 can generate. While this works great for numbers that are factors of 1000 (such as 2, 4, 5, 10, 20, 50, 100, etc), more arbitrary values won't be as neat and organized.

As an arbitrary example, instead of 10 values, we try for 666 (which should return an even spread of numbers ranging from 0 to 665). The results are listed below:

Random%666 generates:


Number    Probability
0    0.20%
1    0.20%
2    0.20%
3    0.20%
4    0.20%
5    0.20%
6    0.20%
7    0.20%
8    0.20%
9    0.20%
10    0.20%
11    0.20%
12    0.20%
13    0.20%
14    0.20%
15    0.20%
16    0.20%
17    0.20%
18    0.20%
19    0.20%
20    0.20%
21    0.20%
22    0.20%
23    0.20%
24    0.20%
25    0.20%
26    0.20%
27    0.20%
28    0.20%
29    0.20%
30    0.20%
31    0.20%
32    0.20%
33    0.20%
34    0.20%
35    0.20%
36    0.20%
37    0.20%
38    0.20%
39    0.20%
40    0.20%
41    0.20%
42    0.20%
43    0.20%
44    0.20%
45    0.20%
46    0.20%
47    0.20%
48    0.20%
49    0.20%
50    0.20%
51    0.20%
52    0.20%
53    0.20%
54    0.20%
55    0.20%
56    0.20%
57    0.20%
58    0.20%
59    0.20%
60    0.20%
61    0.20%
62    0.20%
63    0.20%
64    0.20%
65    0.20%
66    0.20%
67    0.20%
68    0.20%
69    0.20%
70    0.20%
71    0.20%
72    0.20%
73    0.20%
74    0.20%
75    0.20%
76    0.20%
77    0.20%
78    0.20%
79    0.20%
80    0.20%
81    0.20%
82    0.20%
83    0.20%
84    0.20%
85    0.20%
86    0.20%
87    0.20%
88    0.20%
89    0.20%
90    0.20%
91    0.20%
92    0.20%
93    0.20%
94    0.20%
95    0.20%
96    0.20%
97    0.20%
98    0.20%
99    0.20%
100    0.20%
101    0.20%
102    0.20%
103    0.20%
104    0.20%
105    0.20%
106    0.20%
107    0.20%
108    0.20%
109    0.20%
110    0.20%
111    0.20%
112    0.20%
113    0.20%
114    0.20%
115    0.20%
116    0.20%
117    0.20%
118    0.20%
119    0.20%
120    0.20%
121    0.20%
122    0.20%
123    0.20%
124    0.20%
125    0.20%
126    0.20%
127    0.20%
128    0.20%
129    0.20%
130    0.20%
131    0.20%
132    0.20%
133    0.20%
134    0.20%
135    0.20%
136    0.20%
137    0.20%
138    0.20%
139    0.20%
140    0.20%
141    0.20%
142    0.20%
143    0.20%
144    0.20%
145    0.20%
146    0.20%
147    0.20%
148    0.20%
149    0.20%
150    0.20%
151    0.20%
152    0.20%
153    0.20%
154    0.20%
155    0.20%
156    0.20%
157    0.20%
158    0.20%
159    0.20%
160    0.20%
161    0.20%
162    0.20%
163    0.20%
164    0.20%
165    0.20%
166    0.20%
167    0.20%
168    0.20%
169    0.20%
170    0.20%
171    0.20%
172    0.20%
173    0.20%
174    0.20%
175    0.20%
176    0.20%
177    0.20%
178    0.20%
179    0.20%
180    0.20%
181    0.20%
182    0.20%
183    0.20%
184    0.20%
185    0.20%
186    0.20%
187    0.20%
188    0.20%
189    0.20%
190    0.20%
191    0.20%
192    0.20%
193    0.20%
194    0.20%
195    0.20%
196    0.20%
197    0.20%
198    0.20%
199    0.20%
200    0.20%
201    0.20%
202    0.20%
203    0.20%
204    0.20%
205    0.20%
206    0.20%
207    0.20%
208    0.20%
209    0.20%
210    0.20%
211    0.20%
212    0.20%
213    0.20%
214    0.20%
215    0.20%
216    0.20%
217    0.20%
218    0.20%
219    0.20%
220    0.20%
221    0.20%
222    0.20%
223    0.20%
224    0.20%
225    0.20%
226    0.20%
227    0.20%
228    0.20%
229    0.20%
230    0.20%
231    0.20%
232    0.20%
233    0.20%
234    0.20%
235    0.20%
236    0.20%
237    0.20%
238    0.20%
239    0.20%
240    0.20%
241    0.20%
242    0.20%
243    0.20%
244    0.20%
245    0.20%
246    0.20%
247    0.20%
248    0.20%
249    0.20%
250    0.20%
251    0.20%
252    0.20%
253    0.20%
254    0.20%
255    0.20%
256    0.20%
257    0.20%
258    0.20%
259    0.20%
260    0.20%
261    0.20%
262    0.20%
263    0.20%
264    0.20%
265    0.20%
266    0.20%
267    0.20%
268    0.20%
269    0.20%
270    0.20%
271    0.20%
272    0.20%
273    0.20%
274    0.20%
275    0.20%
276    0.20%
277    0.20%
278    0.20%
279    0.20%
280    0.20%
281    0.20%
282    0.20%
283    0.20%
284    0.20%
285    0.20%
286    0.20%
287    0.20%
288    0.20%
289    0.20%
290    0.20%
291    0.20%
292    0.20%
293    0.20%
294    0.20%
295    0.20%
296    0.20%
297    0.20%
298    0.20%
299    0.20%
300    0.20%
301    0.20%
302    0.20%
303    0.20%
304    0.20%
305    0.20%
306    0.20%
307    0.20%
308    0.20%
309    0.20%
310    0.20%
311    0.20%
312    0.20%
313    0.20%
314    0.20%
315    0.20%
316    0.20%
317    0.20%
318    0.20%
319    0.20%
320    0.20%
321    0.20%
322    0.20%
323    0.20%
324    0.20%
325    0.20%
326    0.20%
327    0.20%
328    0.20%
329    0.20%
330    0.20%
331    0.20%
332    0.20%
333    0.20%
334    0.10%
335    0.10%
336    0.10%
337    0.10%
338    0.10%
339    0.10%
340    0.10%
341    0.10%
342    0.10%
343    0.10%
344    0.10%
345    0.10%
346    0.10%
347    0.10%
348    0.10%
349    0.10%
350    0.10%
351    0.10%
352    0.10%
353    0.10%
354    0.10%
355    0.10%
356    0.10%
357    0.10%
358    0.10%
359    0.10%
360    0.10%
361    0.10%
362    0.10%
363    0.10%
364    0.10%
365    0.10%
366    0.10%
367    0.10%
368    0.10%
369    0.10%
370    0.10%
371    0.10%
372    0.10%
373    0.10%
374    0.10%
375    0.10%
376    0.10%
377    0.10%
378    0.10%
379    0.10%
380    0.10%
381    0.10%
382    0.10%
383    0.10%
384    0.10%
385    0.10%
386    0.10%
387    0.10%
388    0.10%
389    0.10%
390    0.10%
391    0.10%
392    0.10%
393    0.10%
394    0.10%
395    0.10%
396    0.10%
397    0.10%
398    0.10%
399    0.10%
400    0.10%
401    0.10%
402    0.10%
403    0.10%
404    0.10%
405    0.10%
406    0.10%
407    0.10%
408    0.10%
409    0.10%
410    0.10%
411    0.10%
412    0.10%
413    0.10%
414    0.10%
415    0.10%
416    0.10%
417    0.10%
418    0.10%
419    0.10%
420    0.10%
421    0.10%
422    0.10%
423    0.10%
424    0.10%
425    0.10%
426    0.10%
427    0.10%
428    0.10%
429    0.10%
430    0.10%
431    0.10%
432    0.10%
433    0.10%
434    0.10%
435    0.10%
436    0.10%
437    0.10%
438    0.10%
439    0.10%
440    0.10%
441    0.10%
442    0.10%
443    0.10%
444    0.10%
445    0.10%
446    0.10%
447    0.10%
448    0.10%
449    0.10%
450    0.10%
451    0.10%
452    0.10%
453    0.10%
454    0.10%
455    0.10%
456    0.10%
457    0.10%
458    0.10%
459    0.10%
460    0.10%
461    0.10%
462    0.10%
463    0.10%
464    0.10%
465    0.10%
466    0.10%
467    0.10%
468    0.10%
469    0.10%
470    0.10%
471    0.10%
472    0.10%
473    0.10%
474    0.10%
475    0.10%
476    0.10%
477    0.10%
478    0.10%
479    0.10%
480    0.10%
481    0.10%
482    0.10%
483    0.10%
484    0.10%
485    0.10%
486    0.10%
487    0.10%
488    0.10%
489    0.10%
490    0.10%
491    0.10%
492    0.10%
493    0.10%
494    0.10%
495    0.10%
496    0.10%
497    0.10%
498    0.10%
499    0.10%
500    0.10%
501    0.10%
502    0.10%
503    0.10%
504    0.10%
505    0.10%
506    0.10%
507    0.10%
508    0.10%
509    0.10%
510    0.10%
511    0.10%
512    0.10%
513    0.10%
514    0.10%
515    0.10%
516    0.10%
517    0.10%
518    0.10%
519    0.10%
520    0.10%
521    0.10%
522    0.10%
523    0.10%
524    0.10%
525    0.10%
526    0.10%
527    0.10%
528    0.10%
529    0.10%
530    0.10%
531    0.10%
532    0.10%
533    0.10%
534    0.10%
535    0.10%
536    0.10%
537    0.10%
538    0.10%
539    0.10%
540    0.10%
541    0.10%
542    0.10%
543    0.10%
544    0.10%
545    0.10%
546    0.10%
547    0.10%
548    0.10%
549    0.10%
550    0.10%
551    0.10%
552    0.10%
553    0.10%
554    0.10%
555    0.10%
556    0.10%
557    0.10%
558    0.10%
559    0.10%
560    0.10%
561    0.10%
562    0.10%
563    0.10%
564    0.10%
565    0.10%
566    0.10%
567    0.10%
568    0.10%
569    0.10%
570    0.10%
571    0.10%
572    0.10%
573    0.10%
574    0.10%
575    0.10%
576    0.10%
577    0.10%
578    0.10%
579    0.10%
580    0.10%
581    0.10%
582    0.10%
583    0.10%
584    0.10%
585    0.10%
586    0.10%
587    0.10%
588    0.10%
589    0.10%
590    0.10%
591    0.10%
592    0.10%
593    0.10%
594    0.10%
595    0.10%
596    0.10%
597    0.10%
598    0.10%
599    0.10%
600    0.10%
601    0.10%
602    0.10%
603    0.10%
604    0.10%
605    0.10%
606    0.10%
607    0.10%
608    0.10%
609    0.10%
610    0.10%
611    0.10%
612    0.10%
613    0.10%
614    0.10%
615    0.10%
616    0.10%
617    0.10%
618    0.10%
619    0.10%
620    0.10%
621    0.10%
622    0.10%
623    0.10%
624    0.10%
625    0.10%
626    0.10%
627    0.10%
628    0.10%
629    0.10%
630    0.10%
631    0.10%
632    0.10%
633    0.10%
634    0.10%
635    0.10%
636    0.10%
637    0.10%
638    0.10%
639    0.10%
640    0.10%
641    0.10%
642    0.10%
643    0.10%
644    0.10%
645    0.10%
646    0.10%
647    0.10%
648    0.10%
649    0.10%
650    0.10%
651    0.10%
652    0.10%
653    0.10%
654    0.10%
655    0.10%
656    0.10%
657    0.10%
658    0.10%
659    0.10%
660    0.10%
661    0.10%
662    0.10%
663    0.10%
664    0.10%
665    0.10%



The problem should be immediately clear: the values ranging from 0-333 each have a 0.20% probability, while values ranging from 334-665 each have a 0.10% probability. If this were an even spread, we should expect any value from 0-332 to be generated only 50% of the time, but instead we see that numbers in this range are generated 66.6% of the time! If we were relying on a number being generated from this range, random%666 would be returning these values TWICE as often as the rest of the spread.

The solution? Rather than using modulo division, we use the following:

floor(X*random/1000.0)

What this does is multiply arbitrary value X by random, then divide by 1000, then floor the result to produce an integer value. This results in a spread ranging from 0-X, but in a much more even fashion. Say we revise our above code to the following:

floor(666*random/1000.0) generates:


Number    Probability
0    0.20%
1    0.20%
2    0.10%
3    0.20%
4    0.10%
5    0.20%
6    0.10%
7    0.20%
8    0.10%
9    0.20%
10    0.10%
11    0.20%
12    0.10%
13    0.20%
14    0.10%
15    0.20%
16    0.10%
17    0.20%
18    0.10%
19    0.20%
20    0.10%
21    0.20%
22    0.10%
23    0.20%
24    0.10%
25    0.20%
26    0.10%
27    0.20%
28    0.10%
29    0.20%
30    0.10%
31    0.20%
32    0.10%
33    0.20%
34    0.10%
35    0.20%
36    0.10%
37    0.20%
38    0.10%
39    0.20%
40    0.10%
41    0.20%
42    0.10%
43    0.20%
44    0.10%
45    0.20%
46    0.10%
47    0.20%
48    0.10%
49    0.20%
50    0.10%
51    0.20%
52    0.10%
53    0.20%
54    0.10%
55    0.20%
56    0.10%
57    0.20%
58    0.10%
59    0.20%
60    0.10%
61    0.20%
62    0.10%
63    0.20%
64    0.10%
65    0.20%
66    0.10%
67    0.20%
68    0.10%
69    0.20%
70    0.10%
71    0.20%
72    0.10%
73    0.20%
74    0.10%
75    0.20%
76    0.10%
77    0.20%
78    0.10%
79    0.20%
80    0.10%
81    0.20%
82    0.10%
83    0.20%
84    0.10%
85    0.20%
86    0.10%
87    0.20%
88    0.10%
89    0.20%
90    0.10%
91    0.20%
92    0.10%
93    0.20%
94    0.10%
95    0.20%
96    0.10%
97    0.20%
98    0.10%
99    0.20%
100    0.10%
101    0.20%
102    0.10%
103    0.20%
104    0.10%
105    0.20%
106    0.10%
107    0.20%
108    0.10%
109    0.20%
110    0.10%
111    0.20%
112    0.10%
113    0.20%
114    0.10%
115    0.20%
116    0.10%
117    0.20%
118    0.10%
119    0.20%
120    0.10%
121    0.20%
122    0.10%
123    0.20%
124    0.10%
125    0.20%
126    0.10%
127    0.20%
128    0.10%
129    0.20%
130    0.10%
131    0.20%
132    0.10%
133    0.20%
134    0.10%
135    0.20%
136    0.10%
137    0.20%
138    0.10%
139    0.20%
140    0.10%
141    0.20%
142    0.10%
143    0.20%
144    0.10%
145    0.20%
146    0.10%
147    0.20%
148    0.10%
149    0.20%
150    0.10%
151    0.20%
152    0.10%
153    0.20%
154    0.10%
155    0.20%
156    0.10%
157    0.20%
158    0.10%
159    0.20%
160    0.10%
161    0.20%
162    0.10%
163    0.20%
164    0.10%
165    0.20%
166    0.10%
167    0.20%
168    0.10%
169    0.20%
170    0.10%
171    0.20%
172    0.10%
173    0.20%
174    0.10%
175    0.20%
176    0.10%
177    0.20%
178    0.10%
179    0.20%
180    0.10%
181    0.20%
182    0.10%
183    0.20%
184    0.10%
185    0.20%
186    0.10%
187    0.20%
188    0.10%
189    0.20%
190    0.10%
191    0.20%
192    0.10%
193    0.20%
194    0.10%
195    0.20%
196    0.10%
197    0.20%
198    0.10%
199    0.20%
200    0.10%
201    0.20%
202    0.10%
203    0.20%
204    0.10%
205    0.20%
206    0.10%
207    0.20%
208    0.10%
209    0.20%
210    0.10%
211    0.20%
212    0.10%
213    0.20%
214    0.10%
215    0.20%
216    0.10%
217    0.20%
218    0.10%
219    0.20%
220    0.10%
221    0.20%
222    0.10%
223    0.20%
224    0.10%
225    0.20%
226    0.10%
227    0.20%
228    0.10%
229    0.20%
230    0.10%
231    0.20%
232    0.10%
233    0.20%
234    0.10%
235    0.20%
236    0.10%
237    0.20%
238    0.10%
239    0.20%
240    0.10%
241    0.20%
242    0.10%
243    0.20%
244    0.10%
245    0.20%
246    0.10%
247    0.20%
248    0.10%
249    0.20%
250    0.10%
251    0.20%
252    0.10%
253    0.20%
254    0.10%
255    0.20%
256    0.10%
257    0.20%
258    0.10%
259    0.20%
260    0.10%
261    0.20%
262    0.10%
263    0.20%
264    0.10%
265    0.20%
266    0.10%
267    0.20%
268    0.10%
269    0.20%
270    0.10%
271    0.20%
272    0.10%
273    0.20%
274    0.10%
275    0.20%
276    0.10%
277    0.20%
278    0.10%
279    0.20%
280    0.10%
281    0.20%
282    0.10%
283    0.20%
284    0.10%
285    0.20%
286    0.10%
287    0.20%
288    0.10%
289    0.20%
290    0.10%
291    0.20%
292    0.10%
293    0.20%
294    0.10%
295    0.20%
296    0.10%
297    0.20%
298    0.10%
299    0.20%
300    0.10%
301    0.20%
302    0.10%
303    0.20%
304    0.10%
305    0.20%
306    0.10%
307    0.20%
308    0.10%
309    0.20%
310    0.10%
311    0.20%
312    0.10%
313    0.20%
314    0.10%
315    0.20%
316    0.10%
317    0.20%
318    0.10%
319    0.20%
320    0.10%
321    0.20%
322    0.10%
323    0.20%
324    0.10%
325    0.20%
326    0.10%
327    0.20%
328    0.10%
329    0.20%
330    0.10%
331    0.20%
332    0.10%
333    0.20%
334    0.20%
335    0.10%
336    0.20%
337    0.10%
338    0.20%
339    0.10%
340    0.20%
341    0.10%
342    0.20%
343    0.10%
344    0.20%
345    0.10%
346    0.20%
347    0.10%
348    0.20%
349    0.10%
350    0.20%
351    0.10%
352    0.20%
353    0.10%
354    0.20%
355    0.10%
356    0.20%
357    0.10%
358    0.20%
359    0.10%
360    0.20%
361    0.10%
362    0.20%
363    0.10%
364    0.20%
365    0.10%
366    0.20%
367    0.10%
368    0.20%
369    0.10%
370    0.20%
371    0.10%
372    0.20%
373    0.10%
374    0.20%
375    0.10%
376    0.20%
377    0.10%
378    0.20%
379    0.10%
380    0.20%
381    0.10%
382    0.20%
383    0.10%
384    0.20%
385    0.10%
386    0.20%
387    0.10%
388    0.20%
389    0.10%
390    0.20%
391    0.10%
392    0.20%
393    0.10%
394    0.20%
395    0.10%
396    0.20%
397    0.10%
398    0.20%
399    0.10%
400    0.20%
401    0.10%
402    0.20%
403    0.10%
404    0.20%
405    0.10%
406    0.20%
407    0.10%
408    0.20%
409    0.10%
410    0.20%
411    0.10%
412    0.20%
413    0.10%
414    0.20%
415    0.10%
416    0.20%
417    0.10%
418    0.20%
419    0.10%
420    0.20%
421    0.10%
422    0.20%
423    0.10%
424    0.20%
425    0.10%
426    0.20%
427    0.10%
428    0.20%
429    0.10%
430    0.20%
431    0.10%
432    0.20%
433    0.10%
434    0.20%
435    0.10%
436    0.20%
437    0.10%
438    0.20%
439    0.10%
440    0.20%
441    0.10%
442    0.20%
443    0.10%
444    0.20%
445    0.10%
446    0.20%
447    0.10%
448    0.20%
449    0.10%
450    0.20%
451    0.10%
452    0.20%
453    0.10%
454    0.20%
455    0.10%
456    0.20%
457    0.10%
458    0.20%
459    0.10%
460    0.20%
461    0.10%
462    0.20%
463    0.10%
464    0.20%
465    0.10%
466    0.20%
467    0.10%
468    0.20%
469    0.10%
470    0.20%
471    0.10%
472    0.20%
473    0.10%
474    0.20%
475    0.10%
476    0.20%
477    0.10%
478    0.20%
479    0.10%
480    0.20%
481    0.10%
482    0.20%
483    0.10%
484    0.20%
485    0.10%
486    0.20%
487    0.10%
488    0.20%
489    0.10%
490    0.20%
491    0.10%
492    0.20%
493    0.10%
494    0.20%
495    0.10%
496    0.20%
497    0.10%
498    0.20%
499    0.10%
500    0.20%
501    0.10%
502    0.20%
503    0.10%
504    0.20%
505    0.10%
506    0.20%
507    0.10%
508    0.20%
509    0.10%
510    0.20%
511    0.10%
512    0.20%
513    0.10%
514    0.20%
515    0.10%
516    0.20%
517    0.10%
518    0.20%
519    0.10%
520    0.20%
521    0.10%
522    0.20%
523    0.10%
524    0.20%
525    0.10%
526    0.20%
527    0.10%
528    0.20%
529    0.10%
530    0.20%
531    0.10%
532    0.20%
533    0.10%
534    0.20%
535    0.10%
536    0.20%
537    0.10%
538    0.20%
539    0.10%
540    0.20%
541    0.10%
542    0.20%
543    0.10%
544    0.20%
545    0.10%
546    0.20%
547    0.10%
548    0.20%
549    0.10%
550    0.20%
551    0.10%
552    0.20%
553    0.10%
554    0.20%
555    0.10%
556    0.20%
557    0.10%
558    0.20%
559    0.10%
560    0.20%
561    0.10%
562    0.20%
563    0.10%
564    0.20%
565    0.10%
566    0.20%
567    0.10%
568    0.20%
569    0.10%
570    0.20%
571    0.10%
572    0.20%
573    0.10%
574    0.20%
575    0.10%
576    0.20%
577    0.10%
578    0.20%
579    0.10%
580    0.20%
581    0.10%
582    0.20%
583    0.10%
584    0.20%
585    0.10%
586    0.20%
587    0.10%
588    0.20%
589    0.10%
590    0.20%
591    0.10%
592    0.20%
593    0.10%
594    0.20%
595    0.10%
596    0.20%
597    0.10%
598    0.20%
599    0.10%
600    0.20%
601    0.10%
602    0.20%
603    0.10%
604    0.20%
605    0.10%
606    0.20%
607    0.10%
608    0.20%
609    0.10%
610    0.20%
611    0.10%
612    0.20%
613    0.10%
614    0.20%
615    0.10%
616    0.20%
617    0.10%
618    0.20%
619    0.10%
620    0.20%
621    0.10%
622    0.20%
623    0.10%
624    0.20%
625    0.10%
626    0.20%
627    0.10%
628    0.20%
629    0.10%
630    0.20%
631    0.10%
632    0.20%
633    0.10%
634    0.20%
635    0.10%
636    0.20%
637    0.10%
638    0.20%
639    0.10%
640    0.20%
641    0.10%
642    0.20%
643    0.10%
644    0.20%
645    0.10%
646    0.20%
647    0.10%
648    0.20%
649    0.10%
650    0.20%
651    0.10%
652    0.20%
653    0.10%
654    0.20%
655    0.10%
656    0.20%
657    0.10%
658    0.20%
659    0.10%
660    0.20%
661    0.10%
662    0.20%
663    0.10%
664    0.20%
665    0.10%



While this isnt foolproof, as we still have values that are twice as likely to be returned than the other values, you'll notice that the spread is much more even. Values ranging from 0-332 now have a 50% chance of being generated, as we would expect them to be, as opposed to the 66.6% probability we were generating before.

Link to comment
Share on other sites

Delving more into the original scenario to shed some more light, notice how I mentioned that using numbers that are factors of 1000 do not cause these problems. This is due to the fact that these numbers evenly divide into 1000. When we take an arbitrary value, such as 666, which does not divide equally into 1000, we have an incomplete wrap-around when we perform modulo division, so certain numbers appear more frequently than others due to this remainder.

 

Since we know performing modulo division using factors of 1000 will provide an even spread of random numbers no matter what, we can actually combine these two methods;

 

floor(X*(random%Y)/(Y*1.0)

 

That *1.0 part at the end is important. Since (Random%Y) will return an integer value, diving it by another integer will produce an integer result. For the purposes of this RNG, we want to divide by a float value so that we get a float value as our result. Y, obviously, is our factor of 1000 (2, 4, 5, 10, etc).

 

Of course, not all factors of 1000 will be appropriate in certain circumstances;

 

floor(300*(random%10)/10.0)

 

In this example, our RNG will return values ranging from 0-299, but because our actual random number spread is so small (returning values ranging from 0.0 to 0.9 in increments of 0.1), this RNG will only produce 10 values of 0, 30, 60, 90, 120, 150, 180, 210, 240, and 270. Thus, when using an RNG like this, be sure that your factor of 1000 is always greater than the number of values that you're looking for in your range of random numbers. Similarly, this same problem will also occur when attempting to generate a range of random numbers greater than 1000 values;

 

floor(5000*random/1000.0)

 

In this case, even the results of random will be too small to give us an even spread of values. So in cases such as these, try to keep your range below 1000 or seek an alternative PRNG for Mugen.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...