Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with pseudo random number generator in python

Status
Not open for further replies.

jeitjes

Programmer
Jun 22, 2006
1
NL
Hi,

I use the pseudo random number generator from python
This is the code

random_number = random()
random_number = int(round(random_number * 99 + 1))

I use this to get random numbers from 1-100. I collected a lot of random numbers this way and analyzed whether every number was drawn aproximately 1% of the time. This is the case EXCEPT for the numbers 1 and 100 which are drawn approximately 0.5 % of the time. What am I doing wrong. Obviously I want all the numbers to be drawn about 1% of the time.

Many thanks in advance,

Jeroen Eitjes
 
I would suggest using randint() instead. Here are the results that I got using randint(1, 100) for many trials:

1 = 1.0023%
2 = 1.0%
3 = 0.9982%
4 = 1.0046%
5 = 0.9953%
6 = 0.9913%
7 = 0.99%
8 = 1.0027%
9 = 1.0009%
10 = 1.0198%
11 = 1.0212%
12 = 1.0047%
13 = 1.0179%
14 = 0.9945%
15 = 0.9853%
16 = 0.9904%
17 = 0.9932%
18 = 0.9972%
19 = 0.9936%
20 = 0.9935%
21 = 0.9968%
22 = 0.9979%
23 = 1.0085%
24 = 0.9909%
25 = 1.0026%
26 = 0.9883%
27 = 1.0093%
28 = 0.9998%
29 = 0.9928%
30 = 1.0084%
31 = 0.9943%
32 = 1.0072%
33 = 1.0219%
34 = 1.007%
35 = 1.0091%
36 = 1.0049%
37 = 1.0036%
38 = 1.011%
39 = 1.0035%
40 = 1.0056%
41 = 0.9968%
42 = 0.9865%
43 = 1.0027%
44 = 1.016%
45 = 0.9977%
46 = 1.0033%
47 = 1.0047%
48 = 0.9837%
49 = 1.0138%
50 = 1.0059%
51 = 0.9968%
52 = 0.9916%
53 = 0.9992%
54 = 0.9996%
55 = 0.997%
56 = 1.0068%
57 = 0.9909%
58 = 1.0097%
59 = 0.9798%
60 = 1.0197%
61 = 1.0082%
62 = 0.9814%
63 = 0.999%
64 = 1.0026%
65 = 0.9906%
66 = 0.9954%
67 = 0.9997%
68 = 1.0164%
69 = 1.0023%
70 = 0.997%
71 = 0.9983%
72 = 0.9998%
73 = 0.9965%
74 = 0.9897%
75 = 1.0031%
76 = 1.0049%
77 = 0.9879%
78 = 1.0102%
79 = 0.9969%
80 = 0.9997%
81 = 0.9746%
82 = 0.9962%
83 = 0.9842%
84 = 1.0141%
85 = 1.0055%
86 = 0.9785%
87 = 1.0164%
88 = 1.0343%
89 = 1.0016%
90 = 1.0067%
91 = 0.9981%
92 = 0.9936%
93 = 0.9763%
94 = 0.9998%
95 = 0.991%
96 = 0.9977%
97 = 1.0001%
98 = 0.9987%
99 = 1.0009%
100 = 0.9899%

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top