Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 18th, 2006, 4:17 PM   #1
Varrickmana
Newbie
 
Varrickmana's Avatar
 
Join Date: May 2006
Posts: 21
Rep Power: 0 Varrickmana is on a distinguished road
Random

How do you do random things with C++. If someone has written a dice roller or random number genorator, if they could post it here that would be great. And maybe explain it a bit. Thanks.
:banana: :banana: :banana: :banana: :banana: <<<Oh my God, it's N'Sync!!! RUN AWAY!!!
__________________
Kids, every year, more than 400,000 people die from tobacco chewing, smoking, an-
MY GOD, HERE COMES A ZOMBIE PIRATE NINJA!!!!!!
Varrickmana is offline   Reply With Quote
Old May 18th, 2006, 4:22 PM   #2
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
Include <cstdlib> and make use of the srand and rand functions. Some info on how to use them can be found here.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old May 18th, 2006, 4:23 PM   #3
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Almost anything is pseudo random, many links can be found on this forum and even more on google, like here.

N'Sync... I thought that group was dead. Justin is singing solo now isn't he?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old May 19th, 2006, 12:54 AM   #4
White-Hat`
Programmer
 
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0 White-Hat` is on a distinguished road
Die Roller

Like this?

// Die Roller

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    srand(time(0));  // seeds the random number generator based on the current time
    
    int randomNumber = rand();  // generate random number
    
    int die = (randomNumber % 6) + 1; // get a number between 1 and 6
    cout << "You rolled a " << die << endl;
    
    return 0;
}

Pretty self explanatory, and a rather simplified version of a dice roller.
White-Hat` is offline   Reply With Quote
Old May 19th, 2006, 11:24 AM   #5
Varrickmana
Newbie
 
Varrickmana's Avatar
 
Join Date: May 2006
Posts: 21
Rep Power: 0 Varrickmana is on a distinguished road
Thx for ur help, guys.
__________________
Kids, every year, more than 400,000 people die from tobacco chewing, smoking, an-
MY GOD, HERE COMES A ZOMBIE PIRATE NINJA!!!!!!
Varrickmana is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:43 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC