Using random numbers

So I might be getting into playing Dungeons and Dragons online with some other folks online.

So I have no idea where my dice are but then it hit me, just import the random function in Python – ask for number of sides on the dice then produce a random number between 1 and that number of sides. Pretty simple. Here’s the code that works the magic:

First:
from random import *

That imports all the random commands.

def diceroll():
print(“Enter number of die sides”)
s=int(input(“Number of sides:”))
print (randint(1,s))
return()

The next to last line – it’s the randint function followed by two numbers, the start point and highest point or number of sides.

Just call diceroll and enter the number of sides. Pretty easy. Plus I’ve made it so the main choices return after each function is called. Need to clean up line spacing and such but here’s the code:


from random import *
def cvttemp():
print (“Enter temperature in Fahrenheit:/n”)
t= float(input (“Temp in F:”))
print (“Temp in C is: “)
print ((t-32)*(5/9))
return()

def cvtdist():
print (“Enter distance in km”)
d=float(input(“Distance in KM:”))
print(((d/2)*.25)+d/2)
return()

def cvtliquid():
print(“Enter volume in liters”)
v=input(“Volume in Liters:”)
print(v)
return()

def diceroll():
print(“Enter number of die sides”)
s=int(input(“Number of sides:”))
print (randint(1,s))
return()
while 1==1:
print (“Prees 1 for F to C”)
# print (“Press 2 for C to F”)
print (“Press 3 for KM to Miles”)
#print (“Press 4 for Miles to KM”)
print (“Press 5 for Liters to Ounces”)
#print (“Press 6 for Ounce to Liters”)
print (“Press 7 for random number generation”)
a = int(input (“Enter your choice now: “))
if a == 1:
cvttemp()
elif a==2:
cvtdist()
elif a==3:
cvtliquid()
elif a==7:
diceroll()

 

One thought on “Using random numbers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s