Streamlabs-Chatbot-Python-Boilerplate icon indicating copy to clipboard operation
Streamlabs-Chatbot-Python-Boilerplate copied to clipboard

random.randint(1, 100) only giving number between 1 and 50.

Open ewanm89 opened this issue 6 years ago • 3 comments

I've written a script to add a little minigame, to calculate a win I'm just using random.randint(1, 100) I noticed I kept getting a lot of results below 50 so I added the following code in execute() to look at the distribution:

	n = 1000000
	c = Counter(random.randint(1, 100) for _ in xrange(n))
	for i in range(1,101):
		Parent.Log(ScriptName,'%2s  %02.10f%%' % (i, c[i] * 100.0 / n))

Now python 2.7 install directly gives a nice even distribution:

Python 2.7.16 (v2.7.16:413a49145e, Mar  4 2019, 01:37:19) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> from collections import Counter
>>> n=1000000
>>> c = Counter(random.randint(1, 100) for _ in xrange(n))
>>> for i in range(1,101):
...     print '%2s  %02.10f%%' % (i, c[i] * 100.0 / n)
...
 1  0.9937000000%
 2  1.0013000000%
 3  1.0080000000%
 4  1.0117000000%
 5  1.0107000000%
 6  1.0049000000%
 7  0.9878000000%
 8  0.9914000000%
 9  0.9895000000%
10  1.0182000000%
11  0.9895000000%
12  0.9918000000%
13  1.0115000000%
14  1.0083000000%
15  1.0141000000%
16  1.0002000000%
17  0.9573000000%
18  0.9942000000%
19  0.9954000000%
20  0.9903000000%
21  1.0079000000%
22  0.9970000000%
23  0.9939000000%
24  1.0002000000%
25  0.9984000000%
26  1.0044000000%
27  1.0141000000%
28  0.9856000000%
29  1.0029000000%
30  0.9988000000%
31  1.0074000000%
32  1.0062000000%
33  1.0025000000%
34  0.9936000000%
35  0.9851000000%
36  0.9965000000%
37  1.0003000000%
38  0.9822000000%
39  0.9903000000%
40  0.9949000000%
41  1.0179000000%
42  0.9972000000%
43  0.9967000000%
44  1.0071000000%
45  0.9921000000%
46  1.0032000000%
47  1.0045000000%
48  1.0018000000%
49  0.9949000000%
50  0.9877000000%
51  0.9945000000%
52  1.0171000000%
53  1.0173000000%
54  1.0102000000%
55  0.9995000000%
56  0.9965000000%
57  0.9921000000%
58  0.9936000000%
59  1.0059000000%
60  0.9854000000%
61  1.0022000000%
62  0.9966000000%
63  1.0086000000%
64  0.9895000000%
65  1.0002000000%
66  1.0057000000%
67  1.0007000000%
68  0.9912000000%
69  1.0103000000%
70  0.9957000000%
71  0.9962000000%
72  0.9985000000%
73  0.9895000000%
74  1.0038000000%
75  1.0107000000%
76  1.0124000000%
77  0.9928000000%
78  0.9966000000%
79  1.0023000000%
80  0.9979000000%
81  0.9971000000%
82  1.0013000000%
83  1.0058000000%
84  1.0029000000%
85  0.9952000000%
86  1.0197000000%
87  0.9936000000%
88  1.0046000000%
89  0.9927000000%
90  1.0276000000%
91  0.9920000000%
92  1.0070000000%
93  0.9911000000%
94  0.9959000000%
95  1.0075000000%
96  1.0066000000%
97  1.0102000000%
98  0.9953000000%
99  0.9961000000%
100  1.0092000000%

however in in the bot every number is only in first half of the range: image

ewanm89 avatar Jul 05 '19 23:07 ewanm89

I confirm this issue. I was testing my code with python 2.7 and it works fine. But when I switched to chatbot, all my randint values where min to max/2.

SL Chatbot v 1.0.2.59

CVex2150J avatar Jul 24 '19 18:07 CVex2150J

I found on StreamLabs Chatbot Discord that :

IAmTomahawkx: Can't use the normal random module. Mersennetwister don't work with IronPython.. src

Issue: https://github.com/IronLanguages/ironpython2/issues/231

Using WichmannHill PNRG fix it for me src

import random
rng=random.WichmannHill(seed)
ri=rng.randint(a,b)

Note that this Wichmann-Hill generator can no longer be recommended: its period is too short by contemporary standards (https://docs.python.org/2/library/random.html)

[Edit] SL Chatbot v 1.0.2.59 still using IronPython 2.7.7.1000 so that's why

CVex2150J avatar Jul 24 '19 18:07 CVex2150J

Yeah, I specifically want to use a Mersenne Twister for the actual reasons of what it is for. It isn't just random randint(a, b) but also randrange does it, but then this is expected as randint just runs randrange(a, b+1) in standard python library. Is there a way we can force it to use the python code version of the library rather than the IronPython version (I assume that is why it is actually happening)

ewanm89 avatar Jul 25 '19 09:07 ewanm89