How to call the random list function without chaning it in Python?
<p>I really need help. I defined a function that gives a x number of rolls for n-sided dice. Now I am asked to calculate the frequency of the each side and <code>freq1 += 1</code> doesn't seem to work considering there can be many more sides (not just 6) and what I did was; </p>

<p>the first function I defined was <code>dice(x)</code>,</p>

<pre><code>freq_list = list(range(1,(n+1)))
rollfreq = [0]*n
for w in dice(x):
    rollfreq[w-1] += 1

print zip(freq_list,rollfreq)
</code></pre>

<p>I get a list such as <code>[(1,0),(2,4),(3,1)...]</code> so on, which is expected, yet the problem is the rollfreq valus doesn't match the original randomly generated <code>dice(x)</code> list. I assume it is because since it is RNG, it changes the values of the <code>dice(x)</code> in the second run as well so that I can not refer to my original randomly generated <code>dice(x)</code> function. Is there any solution to this? I mean I tried almost everything, yet it apparently doesn't work!</p>

<p>EDIT:</p>

<pre><code>import random

n = raw_input('Number of the sides of the dice: ')
n = int(n)
x = raw_input('Number of the rolls: ')
x = int(x)

def dice():
    rolls = []
    for i in range(x):
        rolls.append(random.randrange(1, (n+1)))
    return rolls
print dice()
freq_list = list(range(1,(n+1)))
rollfreq = [0]*n
for w in dice():
        rollfreq[w-1] += 1

print 'The number of frequency of each side:', zip(freq_list,rollfreq)
</code></pre>

<p>I have added the code - hope you guys can help me figure it out this, thank you!</p>

<p>You called the dice() function twice, once when you printed it, and once when you iterated through it in a for loop. The different results come from there.</p>

<pre><code>import random

n = raw_input('Number of the sides of the dice: ')
n = int(n)
x = raw_input('Number of the rolls: ')
x = int(x)

def dice():
    rolls = []
    for i in range(x):
        rolls.append(random.randrange(1, (n+1)))
    return rolls

freq_list = list(range(1,(n+1)))
rollfreq = [0]*n

# Grab the rolls
rolls = dice()
# Print them here
print(rolls)

for w in rolls:
        rollfreq[w-1] += 1

print 'The number of frequency of each side:', zip(freq_list,rollfreq)
</code></pre>

python dice