473,660 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ 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 4119
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
1641
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 like this ('1','2','3') and of course they must not be quoted like this ('1,2,3'). I'm using 'scanf' style substitution into the SQL, eg ...
0
1453
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 not in (1, 2, 3)' >>>
7
2361
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 don't want the request that happens to trigger the need for the new board to have to pay the time cost of generating it. I set up a producer thread that does nothing but generate boards and put them into a length-two Queue (blocking). At the rate...
41
3058
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 degenerate case, and can also be stored. Here's the web-page, with complete source, illustration, and example usage. Take a look-see, and post your critique! http://home.comcast.net/~anglewyrm/hat.html
2
1419
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, I need to spawn a new thread to carry out the "time watch" - and I need to do this in a cross platform (Well Linux/Windows) way ... Any help will be much appreciated. The code (snippet) follows below:
3
13977
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 connection = new SqlConnection(ConfigurationSettings.AppSettings)) {
48
4731
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 can pick up any mistakes/"don't do"s from this code: #include <iostream> #include <cstdlib>
15
2422
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. They (my co-workers) say (with vehement ardor ;) ) that the usage of setjmp() emplyoyed in function"C" that was called from function "B" that was called from function "A" that was called form the main(), will cause havoc in the stack. And it makes...
206
8287
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 footprint which says: "Indeed, one often hears arguments against building exibility into an engineered sys- tem. For example, in the philosophy of the computer language Python it is claimed: \There should be one|and preferably only one|obvious...
0
8341
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8754
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8542
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7362
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4177
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4343
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2760
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 we have to send another system
2
1740
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.