GIDForums  

Go Back   GIDForums > Computer Programming Forums > Python Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rating: Thread Rating: 3 votes, 5.00 average.
  #1  
Old 15-Apr-2006, 15:28
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,671
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Dice rolling program (random number generator)


Here's a program I wrote for use in my Colonial Marines game. It uses a random number generator to simulate dice rolls. Only a few dice combinations are implemented right now but you can change this program if you want to use it. If you discover any problems with it, please let me know.

Python Code:
#############################################################
#Dice_roller.py
#
#Purpose:  A random number generation program that simulates
#  various dice rolls.
#Author:  Cody Jackson
#Date:  4/11/06
#############################################################

import random #randint

def randomNumGen(choice):
    """Get a random number to simulate a d6, d10, or d100 roll."""
    
    if choice == 1: #d6 roll
    	die = random.randint(1, 6)
    elif choice == 2: #d10 roll
        die = random.randint(1, 10)
    elif choice == 3: #d100 roll
        die = random.randint(1, 100)
    else:   #simple error message
        print "Shouldn't be here.  Invalid choice"
    return die

def multiDie(dice_number, die_type):
    """Add die rolls together, e.g. 2d6, 4d10, etc."""
    
#---Initialize variables    
    final_roll = 0
    val = 0
    
    while val < dice_number:
        final_roll += randomNumGen(die_type)
        val += 1
    print final_roll

def test():
    """Test criteria to show script works."""
    
    print "1d6 = ",    
    multiDie(1,1)   #1d6
    print "\n2d6 = ",
    multiDie(2,1)   #2d6
    print "\n3d6 = ",
    multiDie(3,1)   #3d6
    print "\n4d6 = ",
    multiDie(4,1)   #4d6
    print "\n1d10 = ",
    multiDie(1,2)   #1d10
    print "\n2d10 = ",
    multiDie(2,2)   #2d10
    print "\n3d10 = ",
    multiDie(2,2)   #3d10
    print "\n1d100 = ",
    multiDie(1,3)   #d100
    
if __name__ == "__main__":    #run test() if not import as a module
    test()
__________________
Start Programming with Python-A beginner's guide to programming and the Python language.

Interested in pen & paper role playing games from the "golden age" of gaming? Visit Old School Role-Playing Games
  #2  
Old 17-Apr-2006, 17:34
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,671
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: Dice rolling program (random number generator)


I changed the program a little bit to make it "programmer friendly". The new version lets the multiDie() function return an integer, rather than just printing the value. This means you can import the module into another program and use the multiDie() value directly. The test() function has also been modified to account for the change. I also included the usual GPL license information for clarity.
Python Code:
#############################################################
#Dice_roller.py
#
#Purpose:  A random number generation program that simulates
#  various dice rolls.
#Author:  Cody Jackson
#Date:  4/11/06
#
#Copyright 2006 Cody Jackson
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#############################################################

import random #randint

def randomNumGen(choice):
    """Get a random number to simulate a d6, d10, or d100 roll."""
    
    if choice == 1: #d6 roll
    	die = random.randint(1, 6)
    elif choice == 2: #d10 roll
        die = random.randint(1, 10)
    elif choice == 3: #d100 roll
        die = random.randint(1, 100)
    else:   #simple error message
        print "Shouldn't be here.  Invalid choice"
    return die

def multiDie(dice_number, die_type):
    """Add die rolls together, e.g. 2d6, 4d10, etc."""
    
#---Initialize variables    
    final_roll = 0
    val = 0
    
    while val < dice_number:
        final_roll += randomNumGen(die_type)
        val += 1
    return final_roll

def test():
    """Test criteria to show script works."""
    
    _1d6 = multiDie(1,1)   #1d6
    print "1d6 = ", _1d6,    
    _2d6 = multiDie(2,1)   #2d6
    print "\n2d6 = ", _2d6,
    _3d6 = multiDie(3,1)   #3d6
    print "\n3d6 = ", _3d6,
    _4d6 = multiDie(4,1)   #4d6
    print "\n4d6 = ", _4d6,
    _1d10 = multiDie(1,2)   #1d10
    print "\n1d10 = ", _1d10,
    _2d10 = multiDie(2,2)   #2d10
    print "\n2d10 = ", _2d10,
    _3d10 = multiDie(2,2)   #3d10
    print "\n3d10 = ", _3d10,
    _d100 = multiDie(1,3)   #d100
    print "\n1d100 = ", _d100,    
    
if __name__ == "__main__":  #run test() if calling as a separate program
    test()
__________________
Start Programming with Python-A beginner's guide to programming and the Python language.

Interested in pen & paper role playing games from the "golden age" of gaming? Visit Old School Role-Playing Games
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting a number amount to text Godzilla C++ Forum 5 31-Mar-2006 11:38
random number generation issues Jonnyz007 C++ Forum 8 27-Oct-2005 19:13
Need Help with my Cards Program (C++) krisopotamus C++ Forum 2 06-Oct-2005 16:48
Help with interactive program, please nika1p2 C Programming Language 1 09-May-2005 00:22
Anyone can write a program code for this??? chriskan76 C Programming Language 1 19-Oct-2004 20:25

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:53.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.