Click here to Skip to main content
6,305,776 members and growing! (17,681 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Game Development » Games     Intermediate

Random Number Die Roller

By Zachery

Generates random numbers using user constraints.
C#, VC6, VC7, .NET, Win2K, WinXP, Visual Studio, MFC, Dev
Posted:28 May 2003
Views:66,660
Bookmarked:12 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
16 votes for this article.
Popularity: 3.18 Rating: 2.64 out of 5
11 votes, 73.3%
1
1 vote, 6.7%
2
1 vote, 6.7%
3
1 vote, 6.7%
4
1 vote, 6.7%
5

Sample Image - Die_Roller.jpg

Introduction

This program was written for those of you who are into RPG's and other games that require rolling of different types of dice a set number of times.

Setting It Up

The first step to writing this was to create a GUI for the interaction. This GUI would allow the user to specify how many sides the die has and how many times to roll it. For specifying how many sides the die has, I used the Windows Forms Control, ComboBox. I then added the different sides that the die could have (2 - 21*). For how many times to roll, I used a simple Windows Forms Control, TextBox.

Rolling The Die (Updated May 30)

For setting up the code for the die rolling, all you need is a simple Random. First off, you must make sure that if the user specified how many times to roll, and that what he typed in the TextBox is a number. As a default, you may want to make the roll number "1". In testing to see if the roll number is a number, I added a very simple if statement to the txtFrom_KeyPress method (thanks to Richard Day for his suggestion):

if (char.IsNumber(e.KeyChar) == false)
{
    e.Handled = true;
    txtFrom.Text = string.Empty;
}

This ensures that no non-number can be entered into the text box.

Next, I initialize the Random by using the clock's millisecond ticks to ensure as much randomness as possible. Then, depending upon how many times the user has specified that the die be rolled, I generate a random number between 1 and however many sides are specified, a set number of times. Each number is then put into another TextBox and displayed to the user.

Additional

I added a button to add the total of all the numbers rolled together. All I do to accomplish this is set up an array of type int and fill it as I generate the numbers. Then, I simply iterate through the array, adding each of the numbers together and then displaying that in another text box.

for (int i = 0; i < numbers.Length; i++)
     total += numbers[i];

*Note: While there may be no 21 sided die, I decided to add it anyways.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Zachery


Member
"..Been walking the earth since child-birth. I never went to school, and when I did I ate my homework. Never played it cool, and if I did I was a poser. You can't blame a nerd for trying to get closer. Now I'm the poster boy for geeks and freaks..."

My profile according to Google

I like tha moon

All Your Base Are Belong To Us

Think different, think beige.

Domopers

Disclaimer:

Any and all things I say, I in no way think myself better then anyone else. I have my fair share (well, a good deal) of problems. I just like to gripe whenever given the chance

Occupation: Web Developer
Location: United States United States

Other popular Game Development articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 11 of 11 (Total in Forum: 11) (Refresh)FirstPrevNext
Jokehi Pinmemberhcjd22:48 24 Aug '08  
GeneralPah Pinmemberdog_spawn10:41 29 May '03  
GeneralRe: Pah PinmemberZachery10:45 29 May '03  
GeneralRe: Pah Pinmemberdog_spawn11:31 29 May '03  
GeneralRe: Pah PinmemberZachery11:47 29 May '03  
GeneralRe: Pah Pinmemberdog_spawn12:05 29 May '03  
GeneralRe: Pah PinmemberZachery21:02 29 May '03  
GeneralRe: Non-Simple Int Checkers PinmemberZachery11:01 29 May '03  
GeneralRe: Non-Simple Int Checkers Pinmemberdog_spawn11:32 29 May '03  
GeneralRe: Pah PinmemberRichard Day23:56 29 May '03  
GeneralRe: Pah PinmemberZachery4:35 30 May '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 May 2003
Editor: Smitha Vijayan
Copyright 2003 by Zachery
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project