+ Reply to Thread
Results 1 to 1 of 1

Thread: Python randomness

  1. #1
    like2program is offline x10 Sophmore like2program is an unknown quantity at this point
    Join Date
    Nov 2007
    Location
    In a hole at the bottom of the sea
    Posts
    244

    Python randomness

    I am currently working on a python script to generate random dice rolls of two dice. It simulates 100 dice rolls and then takes the average or die1 die2 and the sum of the dice. It then outputs the results to a file in a "numbers" folder for 100 files. My problem is that each of the dies have a perfect 3 average and the sum is a perfect 6. How might I be able to make the results more random? (I want the averages to be a little off, which requires that the numbers be a little more random)


    The Code
    Code:
    #!/usr/bin/python
    # Filename: randomdice.py
    #Copyright: Like2program 2008
    
    import random
    
    aavg = 0
    bavg = 0
    cavg = 0
    
    
    
    for y in range(1,101):
        filename = "numbers/numbers"+str(y)+".txt"
        open(filename, "w").write('# Dice 1 + Dice 2 = Sum:\n')
        aavg = 0
        bavg = 0
        cavg = 0
        for x in range(1, 101):
            random.seed()        
            a = random.randint(1,6)
            random.seed()
            b = random.randint(1,6)
            c = a+b
            aavg = aavg + a
            bavg = bavg + b
            cavg = cavg + c
            spam = str(x)+".  "+str(a) + "    +   " + str(b) + "    =   " + str(c) + "\n"
            open(filename, "a").write(spam)
        aavg = aavg/100
        bavg = bavg/100
        cavg = cavg/100
        spam = "Die 1 average: " + str(aavg) + " -- Die 2 average: " + str(bavg) + " -- Sum average:" + str(cavg)
        open(filename, "a").write(spam)
    It generates a fairly clean text file with the results.
    Another problem is that is sometimes makes a result with the sum listed as 7 when it is in fact 6. Help would be appreciated.

    Thanks,
    ~like2
    Edit:
    Well, I have solved this mystery all on my own, using the wonderful tool that is floating point numbers!

    By changing the averages initial values, I was able to make them floating points, and they now work just fine!

    The working code is below
    Code:
    #!/usr/bin/python
    # Filename: randomdice.py
    #Copyright: Like2program 2008
    
    import random
    
    aavg = 0.0
    bavg = 0.0
    cavg = 0.0
    
    
    
    for y in range(1,101):
        filename = "numbers/numbers"+str(y)+".txt"
        open(filename, "w").write('# Dice 1 + Dice 2 = Sum:\n')
        aavg = 0.0
        bavg = 0.0
        cavg = 0.0
        for x in range(1, 101):
            random.seed()        
            a = random.randint(1,6)
            random.seed()
            b = random.randint(1,6)
            c = a+b
            aavg = aavg + a
            bavg = bavg + b
            cavg = cavg + c
            spam = str(x)+".  "+str(a) + "    +   " + str(b) + "    =   " + str(c) + "\n"
            open(filename, "a").write(spam)
        aavg = aavg/100
        bavg = bavg/100
        cavg = cavg/100
        spam = "Die 1 average: " + str(aavg) + " -- Die 2 average: " + str(bavg) + " -- Sum average:" + str(cavg)
        open(filename, "a").write(spam)
    Last edited by like2program; 10-11-2008 at 09:07 AM. Reason: Automerged Doublepost

    - Useful Links -
    || Free hosting Support || Request Unsuspension || Sign up for Free Hosting || Rules and Regulations ||
    || The x10 Commandments || Terms of Service || How do the credits work? ||


    If you found this useful or interesting hit the blue tick or donate some credits!

    My site is worth



+ Reply to Thread

Similar Threads

  1. Replies: 5
    Last Post: 07-10-2009, 03:30 AM
  2. Python, SQlite and Django?
    By phineas in forum Free Hosting
    Replies: 2
    Last Post: 08-17-2008, 02:02 AM
  3. Python / MySQLdb
    By Korexio in forum Free Hosting
    Replies: 9
    Last Post: 11-29-2007, 08:29 AM
  4. Python/.htaccess help
    By *Face in forum Free Hosting
    Replies: 6
    Last Post: 09-21-2007, 01:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers