473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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 4106
ilikepython
844 Expert 512MB
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 Expert 4TB
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
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.