We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,956 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

C++ Random number generator

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?

6
Contributors
7
Replies
2 Years
Discussion Span
1 Year Ago
Last Updated
12
Views
Question
Answered
logicmonster
Light Poster
37 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

NathanOliver
Posting Virtuoso
1,515 posts since Apr 2009
Reputation Points: 281
Solved Threads: 277
Skill Endorsements: 3

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

logicmonster
Light Poster
37 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

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!

logicmonster
Light Poster
37 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 3 Years Ago by WaltP and NathanOliver

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/

silvy
Newbie Poster
2 posts since Mar 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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

Shanzeh Sohail
Newbie Poster
1 post since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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 ).

MrEARTHSHAcKER
Junior Poster in Training
77 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0796 seconds using 2.69MB