1

When writing this code I found myself doing a lot of repetitive stuff and was wondering if there is an easier or simpler or shorter way to do a repetitive task such as this.

Here is the relevant code:

 from random import randint
    RandomNumber = Zeroes = Ones = Twos = Threes = Fours = Fives = Sixes = i = 0

    while i < 1000000:
        RandomNumber = (randint(0,6))
        if RandomNumber == 0:
            Zeroes = Zeroes + 1
        if RandomNumber == 1:
            Ones = Ones + 1
        if RandomNumber == 2:
            Twos = Twos + 1
        if RandomNumber == 3:
            Threes = Threes + 1
        if RandomNumber == 4:
            Fours = Fours + 1
        if RandomNumber == 5:
            Fives = Fives + 1
        if RandomNumber == 6:
            Sixes = Sixes + 1

        i = i + 1
abhinsit
  • 3,214
  • 4
  • 21
  • 26

6 Answers6

1

here you go...

from random import randint
outcomes=[0]*7
for i in range(1000000):
    outcomes[randint(0,6)]+=1
karakfa
  • 66,216
  • 7
  • 41
  • 56
1

Rather than taking the named variables for each random output, you can take dictionary with each possible value as key. This will shorten you code and made it extendable for any random range

from random import randint
randomMax = 6
randomList= {i:0 for i in range(0,randomMax+1)}
totalIterations = 10000
while totalIterations >0:
    randomList[randint(0,randomMax)]+=1
    totalIterations-=1

Sample Output:

{0: 1400, 1: 1400, 2: 1500, 3: 1400, 4: 1500, 5: 1000, 6: 1800}
JRodDynamite
  • 12,325
  • 5
  • 43
  • 63
abhinsit
  • 3,214
  • 4
  • 21
  • 26
  • Exactly what I was looking for! Thank you very much –  Aug 08 '16 at 17:40
  • I have made it even more configurable, wherein you can just change the randomMax value and get results for 0->randomMax range – abhinsit Aug 08 '16 at 17:43
  • This works, but takes far, far longer than is actually necessary. See my answer for a constant time solution that doesn't require iterating 10000 times. – James Aug 08 '16 at 17:45
0

First, when you find yourself using tons of variables that suit the same purpose (e.g. number of zeros, ones, twos, etc), you almost certainly need an array (a list in Python terminology).

import random

nums = [0] * 7 # number of digits from zero to six

for i in range(1000001):
    r = random.randint(0, 6) # you can get rid of this variable 
                             # and use random.randint(0, 6) as an array index
    nums[r] += 1 # each member of a list represents a number of some digit's occurrences

print(''.join("{} -> {}\n".format(a, b) for a, b in zip(range(7), nums)))

If you want something extremely short, fast and powerful:

import random, collections

print(''.join("{} -> {}\n".format(a, b) for a, b in collections.Counter(random.randint(0, 6) for _ in range(100)).items()))

Check this to see how one can count occurrences of items of a list with collections.Counter.

This code is fast and doesn't waste memory as it uses generators.

Community
  • 1
  • 1
ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • 1
    I'd make the first line `nums = [0] * 7`, it seems cleaner (I don't really understand why it's range(11) in the first place) – James Aug 08 '16 at 17:34
  • @James, good idea! (This was to count the occurrences of each digit from zero to nine, for some reason :P) – ForceBru Aug 08 '16 at 17:38
0

in your special case you can do

from random import randint
results=[0]*7 # same as [0,0,0,0,0,0,0]

i=0
while i < 1000000:
    n = randint(0,6)
    results[n]+=1
    i+=1

for i in range(len(results)):
    print(i, results[i])
janbrohl
  • 2,626
  • 1
  • 17
  • 15
0

This code might help, counter dictionary will hold numbers as keys, with number occurrences as values.

from random import randint

counter = {}

while i < 1000000:
    RandomNumber = (randint(0,6))
    if RandomNumber in counter:
        counter[RandomNumber] += 1
    else:
        counter[RandomNumber] = 1

    i += 1
Dulmandakh
  • 21
  • 2
0

These answers all work, but they are going to take a very long time to loop when in reality you don't need to use a loop at all. Here's code that takes constant time rather than needs to loop over a million times:

from random import random

nums = [random() for _ in range(7)]
nums = [int((x / sum(nums)) * 1000000) for x in nums]

Admittedly this method will be a little off of 1000000 in total, but it's much, much faster, and you can also add a bit to random ones to make it actually 1000000.

For more info, see:

Getting N random numbers that the sum is M

Random numbers that add to 100: Matlab (For info on the statistical distribution that this generates)

Community
  • 1
  • 1
James
  • 2,843
  • 1
  • 14
  • 24