1,105,352 Community Members

C++ Random number generator

Member Avatar
logicmonster
Light Poster
37 posts since Feb 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I am having a problem displaying random numbers, I'm trying to make a dice game where you play against the computer and the first one to 100 wins. However, my random number is never random, it always comes out as the same number.

From what I understand to simply display a random number in the console is,

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
  int i;

  i = rand();
  cout << i;

  return 0;
}

The problem is that I ALWAYS get the number 41 from this program, no matter how many times i run it.

I've changed it to work like a die;

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
  int i;

  i = (rand()%6)+1;
  cout << i;

  return 0;
}

And from that, I always get the number 6. Basically its not coming out as a random number and I can't figure out why. Can anyone tell me what I'm doing wrong?

Member Avatar
NathanOliver
Posting Virtuoso
1,657 posts since Apr 2009
Reputation Points: 284 [?]
Q&As Helped to Solve: 310 [?]
Skill Endorsements: 4 [?]
 
0
 

You need to use srand() to seed rand before you use it. For a game like yours i would use srand((unsigned)time(0)); . You will need to use #include <ctime> in your code to do this.

Member Avatar
logicmonster
Light Poster
37 posts since Feb 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks for the reply Nathan, but could you perhaps give me a simple example of that? I've never used that command and it would really help if I could see it in use

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

Doen't your book explain it? Or your tutorial site? What about Google?

Member Avatar
logicmonster
Light Poster
37 posts since Feb 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
1
 

Alright, I think I got it,

#include <cstdlib> 
#include <ctime> 
#include <iostream>
 
using namespace std;
 
int main() 
{ 
    srand((unsigned)time(0)); 

    int i;
	i = (rand()%6)+1; 

	cout << i << "\n"; 

}

Thanks for the help!

Question Answered as of 4 Years Ago by WaltP and NathanOliver
Member Avatar
silvy
Newbie Poster
2 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
-1
 

To solve the problem you can use "ctime" library and the function srand().

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
int i, r;

srand(time(0));

for(i = 0; r <= 20000; i++)
r = rand();

cout << "Number is " << r << ". It was generated on try number " << i << ".\n";

return 0;
}

just look here:

http://www.cplusplus.com/forum/beginner/5131/

Member Avatar
Shanzeh Sohail
Newbie Poster
1 post since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i am making hangman game and i want the program to pick up random wrds... how can i do it???

Member Avatar
MrEARTHSHAcKER
Junior Poster in Training
77 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

i am making hangman game and i want the program to pick up random wrds... how can i do it???

I haven't done anything similar to it, but you'd need database which contains words.
And you'll need some "Input/Output with files" skills to get words from database ( for example some *.txt file which contains words, unless you are going to make a huge code by writing all words in a code...within some struct... sounds bad idea ).

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: