473,465 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

"rolling a dice" in python. can it be done?

7 New Member
simple question. is it possible to write a script that rolls a six sided dice?

if this is possible, is it possible to tell it to roll the six sided dice 4 times and ignore the dice that rolled the lowest score?

if you want to know, i am working on a script to create a dungeons and dragons character :)
May 28 '07 #1
4 4108
ilikepython
844 Recognized Expert Contributor
simple question. is it possible to write a script that rolls a six sided dice?

if this is possible, is it possible to tell it to roll the six sided dice 4 times and ignore the dice that rolled the lowest score?

if you want to know, i am working on a script to create a dungeons and dragons character :)
Ofcourse it is possible, python can do anything! You can use the random module. it has several functions having to do with anything random. Try this link:
http://docs.python.org/lib/module-random.html
May 28 '07 #2
bartonc
6,596 Recognized Expert Expert
simple question. is it possible to write a script that rolls a six sided dice?

if this is possible, is it possible to tell it to roll the six sided dice 4 times and ignore the dice that rolled the lowest score?

if you want to know, i am working on a script to create a dungeons and dragons character :)
Just search TheScripts for "python dice". There are plenty of examples of this solution.
May 28 '07 #3
Joshua Brooks
3 New Member
Python. Of course it's possible :)
No claims that this is efficient/pretty/best practise it just does the job.

from random import randrange
diceroll = [0, 0, 0, 0] #initialise a holder for dice rolls
i = 0 #counter for number of rolls
while i < 4: #loop through a dice throw routine
diceroll[i] = randrange(1,7)
i += 1
diceroll.sort() #sort lowest to highest
diceroll = diceroll[1:] #drop the lowest number
print diceroll
Dec 6 '10 #4
Sean Pedersen
30 New Member
Joshua and others are right; of course it can be done! I've modified Joshua's code a bit...
Expand|Select|Wrap|Line Numbers
  1. diceroll = [] #initialise a holder for dice rolls
  2. i = 0 #counter for number of rolls
  3. for i in range(4): #loop through a dice throw routine
  4.     diceroll.append(randrange(1,7))
  5.  
  6. diceroll.sort() #sort lowest to highest
  7. diceroll = diceroll[1:] #drop the lowest number
  8. print diceroll 
Dec 6 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Richard Shea | last post by:
Hi - I've writing a Python script which has a query which looks like this ... select * from T where C1 not in (1,2,3) .... C1 is a numeric column so elements of (1,2,3) must not be quoted...
0
by: Holger Joukl | last post by:
You could use the str() builtin, returning the string representation of the list object: >>> params = (1, 2, 3) >>> "select * from T where C1 not in %s" % str(params) 'select * from T where C1...
7
by: Evan Simpson | last post by:
WEBoggle needs a new game board every three minutes. Boards take an unpredictable (much less than 3min, but non-trivial) amount of time to generate. The system is driven by web requests, and I...
41
by: AngleWyrm | last post by:
I have created a new container class, called the hat. It provides random selection of user objects, both with and without replacement, with non-uniform probabilities. Uniform probabilities are a...
2
by: Alfonso Morra | last post by:
Hi, I am writing a timer class that I want to be able to get to notify me (via a callback func), when a specified interval has elapsed. I have most of the timer functionality figured - however,...
3
by: Nils Magnus Englund | last post by:
Hi, I've made a HttpModule which deals with user authentication. On the first request in a users session, it fetches data from a SQL Server using the following code: using (SqlConnection...
48
by: mahurshi | last post by:
I am new to c++ classes. I defined this "cDie" class that would return a value between 1 and 6 (inclusive) It runs fine and gives no warnings during compilation. I was wondering if you guys...
15
by: rover8898 | last post by:
Hello all, I used setjmp() in a recent of program of mine (it is not completed, so I have not the chance to test it out yet). I am not very profocient in C coding as are some of my co-workers....
206
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.