473,325 Members | 2,785 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,325 software developers and data experts.

srand()

So I've fixed pretty much all of my problems for my program which is supposed to seed the random number generator for rolling two dice. My program compiles, but I cannot get the random numbers to happen, and I've tried several different things. I've included all of my code, but really the only thing I can't get to work right is the random number generator, so any help would be great.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <ctime>
  5. const int SIX = 6; 
  6.  
  7.  using namespace std;
  8.  int main ()
  9.  {
  10.      int seed = (int)time(0);
  11.      srand(seed);
  12.      char gamePlay;
  13.      char playAgain;
  14.      char notAgain;
  15.      char yesSir;
  16.  
  17.      cout << "Do you want to roll the dice?\n";
  18.      cout << "Enter y or n: ";
  19.      cin >> gamePlay;
  20.      cin.ignore( 80, '\n' );
  21.     switch (gamePlay)
  22.     {
  23.         case 'y':
  24.         case 'Y':
  25.             break;
  26.         case 'n':
  27.         case 'N':
  28.             cout <<"Have a nice day." << endl;
  29.             system("PAUSE");
  30.             return 'E';
  31.         default:
  32.             cout <<"Invalid input, please re-enter!" << endl;
  33.             system("PAUSE");
  34.             return 'E';
  35.     }
  36.          do
  37.          {
  38.              int d1 = 1 + seed % SIX; 
  39.              int d2 = 1 + seed % SIX;
  40.              if (d1 == 6 && d2 == 6) 
  41.                  cout <<"You rolled boxcars\n";
  42.              if (d2 == 1 && d2 == 1) 
  43.                   cout <<"You rolled snake-eyes\n";
  44.              else 
  45.                 cout <<"You rolled " << d1 << " and " << d2 <<endl;
  46.                 cout << "Do you want to play again?\n";
  47.                 cout <<"Enter y or n: ";
  48.                 cin >> yesSir;
  49.                 cin.ignore( 80, '\n');
  50.  
  51.             switch (yesSir)
  52.             {
  53.                 case 'y':
  54.                 case 'Y':
  55.                     break;
  56.                 case 'n':
  57.                 case 'N':
  58.                     return 'N';
  59.                     break;
  60.                 default:
  61.                     cout <<"Invalid input, please re-enter!" << endl;
  62.                     system("PAUSE");
  63.                     return 'E';
  64.             }
  65.  
  66.          }while (yesSir != 'N');
  67.  
  68.      system ("PAUSE");
  69.      return 0;
Dec 14 '08 #1
2 1785
Ganon11
3,652 Expert 2GB
seed is not a random function. It is not a random number. seed is an int that you told to be the value of time(0).

To get random numbers, use the rand() function.
Dec 14 '08 #2
Tassos Souris
152 100+
You can write this for example:
Expand|Select|Wrap|Line Numbers
  1. /* At the beginning of your program */
  2. srand( (unsigned int)time( NULL ) );
  3.  
  4. /* more lines here */
  5. int randomNumber = rand();
  6.  
The above is how to combine the srand() and rand() functions.

To take a random number in the range of [ 1, 6 ]:
Expand|Select|Wrap|Line Numbers
  1. int dieFaceValue = 1 + rand() % 6;
  2.  
Dec 14 '08 #3

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

Similar topics

14
by: David Shaw | last post by:
Hi, I'm writing a fun little program to play "Petals Around the Roses" with... I need to seed my random numbers so that they won't be the same every time I run the program, but my compiler won't...
1
by: Intaek LIM | last post by:
generally, we use srand(time(0)) to generate random numbers. i know why we use time(0), but i can not explain how it operates. first, see example source below. ...
13
by: Jeremy Holdstadt | last post by:
This seems to be a C question to me. If it is not, I apologize. This command: awk 'BEGIN {srand();print srand()}' will give the number of seconds since the epoch, present time. Can any of you...
25
by: Merrill & Michele | last post by:
Many of us watched the World Series of Poker this last week and plotted how we were to take over that world. My current problem begins with shuffling the deck. For the apps I've written before,...
3
by: bobrics | last post by:
Hi, I am using srand() and would like to create different random numbers during a SINGLE execution of my program because I want to compare random cases. For now I have a switch statement within a...
11
by: jtagpgmr | last post by:
I am currently using the gcc compiler on a cygwin platform, I am a beginner when it comes to programming in C and want to know why anytime I run the .exe with the following code I get a...
9
by: Chelong | last post by:
Hi All I am using the srand function generate random numbers.Here is the problem. for example: #include<iostream> #include <time.h> int main() {
2
by: Mara Guida | last post by:
"Each time I run my program, I get the same sequence of numbers back from rand()." The answer to question 13.17, in the c-faq is: #include <stdlib.h> #include <time.h> srand((unsigned...
8
by: Ioannis Vranos | last post by:
Is srand(time(0)); an effective solution for seeding rand(), or is there any better approach?
12
by: Bill Cunningham | last post by:
I have read and studied and looked at references and can't find out how to do this. Here's where some real knowledge comes in now lets see who in clc really knows there stuff ;) main(void) {...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.