1

I generated 1,000,000 random number draws using the random module

import random
liste = []
for i in range(0, 1000000):
liste.append(random.uniform(0,1))

Now I have to store the values obtained in a histogram at 6 bins using the bins option. And using if/else conditions on the list items, write a algorithm to simulate the result of 1,000,000 dice rolls. Then I have to calculate the frequency of occurrence of each value and compare it to the expected probability p = 1/6.

Does anyone know how to do this please I have no idea...

Thanks in advance!

edit: concerning the bins options

they talk about this :

import matplotlib.pyplot as plt
import numpy as np
# Création de la figure et des axes
fig, axs = plt.subplots(1, 1, sharey=True, tight_layout=True)
# Ajout de l'histogramme
axs.hist(valeurs, bins=np.arange(0.5, 7.5, 1), ec="black")
axs.set_title("Résultats jet dé")
axs.set_xlabel("Valeur dé")

gives me this

After using @Roland_Smith's program it gives me this

 #import random
 #liste = []
 #for i in range(0, 1000000):
 #    liste.append(random.uniform(0,1))
 import random
 liste = random.choices((1,2,3,4,5,6), k=1000000)
 import collections
 c = collections.Counter(liste)
 print(c)

 p = [j/1e6 for j in c.values()]
 print(p)

 import matplotlib.pyplot as plt
 import numpy as np
 # Création de la figure et des axes
 fig, axs = plt.subplots(1, 1, sharey=True, tight_layout=True)
 # Ajout de l'histogramme
 axs.hist(c, bins=np.arange(0.5, 7.5, 1), ec="black")
 axs.set_title("Résultats jet dé")
 axs.set_xlabel("Valeur dé")

but the problem is that my histogram looks like this enter image description here

is it normal because each number has the same probability of appearing or is it not normal?

edit for tdy

import matplotlib.pyplot as plt
import numpy as np
# Création de la figure et des axes
fig, axs = plt.subplots(1, 1, sharey=True, tight_layout=True)
# Ajout de l'histogramme
axs.hist(c,ec="black")
axs.set_title("Résultats jet dé")
axs.set_xlabel("Valeur dé")

enter image description here

edit after help for @JohanC

#import random
#liste = []
#for i in range(0, 1000000):
#    liste.append(random.uniform(0,1))
import random
liste = random.choices((1,2,3,4,5,6), k=1000000)
import collections
c = collections.Counter(liste)
print(c)

p = [j/1e6 for j in c.values()]
print(p)

import matplotlib.pyplot as plt
import numpy as np
plt.bar(c.keys(), c.values())

# Création de la figure et des axes
#fig, axs = plt.subplots(1, 1, sharey=True, tight_layout=True)
# Ajout de l'histogramme
#axs.hist(c,ec="black")
#axs.set_title("Résultats jet dé")
#axs.set_xlabel("Valeur dé")

enter image description here

i get a value of 160000 for each dice face, i.e. 6. but 6*160000+960000 i shouldn't get the number of rolls, i.e. 1000000?

  • "*histogram at 6 bins using the bins option*" What is the "*bins option*"? What is "*histogram*" is this context? Are you maybe using `r`? – JonSG Feb 12 '22 at 19:05
  • 1
    i uptaded my code, you can see about what they mentionned with "option bins"... thanks for your help :) – Aspiring Physicist Feb 12 '22 at 19:09
  • [`Axes.hist` returns the values, bins, and bars](https://stackoverflow.com/a/70949207/13138364) so you can do: `counts, bins, bars = axs.hist(valeurs, bins=np.arange(0.5, 7.5, 1), ec="black")` – tdy Feb 12 '22 at 19:14
  • @JonSg like this : axs.hist(c,bins=np.arange(0.5, 7.5, 1),ec="black") ? – Aspiring Physicist Feb 12 '22 at 19:31
  • @tdy I want the plot! but how to put the new values obtained with counter values in this histogram? – Aspiring Physicist Feb 12 '22 at 19:35
  • You mean how to label the histogram's bars? Use the `bar_label` method (requires matplotlib version 3.4.0+). See https://stackoverflow.com/a/71095250/13138364 – tdy Feb 12 '22 at 19:41
  • @tdy look I updated my topic, you can see what my problem is with the histogram... :( – Aspiring Physicist Feb 12 '22 at 19:49
  • 1
    You shouldn't mix `Counter` with `hist`. They both count+bin the data. If you use `Counter`, just plot it with a regular `bar` plot. If you use the raw data, plot with `hist`. – tdy Feb 12 '22 at 19:50
  • @tdy I'm not sure I understand, I still have a problem with the display of bars, I updated the topic... – Aspiring Physicist Feb 12 '22 at 19:58
  • "i get a value of 160000 for each dice face, i.e. 6. but 6*160000+960000 i shouldn't get the number of rolls, i.e. 1000000?" No, the bars are a little higher than the 160000 tick, as they should be. They will not be the exact same values, either, because of the randomness, but they will be close enough that you might not see the difference on a plot like this. – Karl Knechtel Feb 12 '22 at 22:11

1 Answers1

1

Dice rolls produce an integer value.

So using random.uniform (which returns a float) is not the right way.

Try:

import random
liste = random.choices((1,2,3,4,5,6), k=1000000)

Then, I'd use collections.Counter to count the values:

import collections
c = collections.Counter(liste)
print(c)

This would get you something like:

Counter({6: 167399, 5: 167083, 2: 166789, 3: 166548, 1: 166236, 4: 165945})

Calculating the probability:

p = [j/1e6 for j in c.values()]

This yields:

[0.167083, 0.165945, 0.167399, 0.166789, 0.166548, 0.166236]

For plotting;

import matplotlib.pyplot as plt

plt.bar(c.keys(),p)
plt.show()

It will look something like this:

plot of dice probability

The reason that all bars look about the same is because they are.

Let's check the difference between p and the probability of a fair dice, which is 1/6:

[round(j-1/6, 6) for j in p]

This gives:

[0.000416, -0.000722, 0.000732, 0.000122, -0.000119, -0.000431]

So the actual counted probability is pretty close to that of a fair dice.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94