11 Replies - 446 Views - Last Post: 11 February 2013 - 11:47 AM Rate Topic: -----

#1 SplinteredChaos  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 112
  • Joined: 15-September 11

Same random numbers generated each time through

Posted 07 February 2013 - 01:13 AM

I'm having some difficulties with the program. The problem is that each time the program is run (meaning within the while loop) the same numbers are generated. I'm not quite sure why. I've done some other Google searching and common issues were people using rand() more than once or inside of a loop, with not seeding the generator in a close second. the call for rand() is well before the loop and only done once. I've also used the standard method of seeding with the time(0). Not quite sure where I need to go to make each time through a different set of numbers be generated.

*side note while this is an assignment, the loop is not a requirement of the assignment, I just have to generate a random set of numbers (like in a dice game).

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

int main()
{
	srand(static_cast<unsigned int>(time(0)));
	int randomNumber = rand();
	
	
	int die1 =(randomNumber % 6) + 1;
	int die2 =(randomNumber % 3) + 3;
	int die3 =(randomNumber % 4) + 2;
	int die4 =(randomNumber % 6) + 1;
	int die5 =(randomNumber % 2) + 4;
	string answer;

	cout << " ***** " << "  " << " ***** " << "  " << " ***** " <<
		    "  "      <<  " ***** " << "  " << " ***** " << endl; //top of dice 1-5
	
	cout << " *   * " << "  " << " *   * " << "  " << " *   * " <<
		    "  "      <<" *   * " << "  " << " *   * " << endl;
	
	cout << " * " << die1 << " * " <</*die 1 */
		    "  "  << " * " << die2<< " * " /* die 2 */
		 << "  " << " * " << die3 << " * " <</* die 3 */
		    "  " << " * " << die4 << " * " <</* die 4 */ 
			"  " << " * " << die5 << " * " <</* die 5 */ endl;
	
	cout << " *   * " << "  " << " *   * " << "  " << " *   * " <<
		   "  "      << " *   * " << "  " << " *   * " << endl;
	
	cout << " ***** " << "  " << " ***** " << "  " << " ***** " <<
		    "  "      <<" ***** " << "  " << " ***** " << endl; //bottom of dice 1-5
	cout << "Do you wish to reroll the dice?" << endl;
	cin >> answer; 
	while (answer =="y")
	{

	cout << " ***** " << "  " << " ***** " << "  " << " ***** " <<
		    "  "      <<  " ***** " << "  " << " ***** " << endl; //top of dice 1-5
	
	cout << " *   * " << "  " << " *   * " << "  " << " *   * " <<
		    "  "      <<" *   * " << "  " << " *   * " << endl;
	
	cout << " * " << die1 << " * " <</*die 1 */
		    "  "  << " * " << die2<< " * " /* die 2 */
		 << "  " << " * " << die3 << " * " <</* die 3 */
		    "  " << " * " << die4 << " * " <</* die 4 */ 
			"  " << " * " << die5 << " * " <</* die 5 */ endl;
	
	cout << " *   * " << "  " << " *   * " << "  " << " *   * " <<
		   "  "      << " *   * " << "  " << " *   * " << endl;
	
	cout << " ***** " << "  " << " ***** " << "  " << " ***** " <<
		    "  "      <<" ***** " << "  " << " ***** " << endl; //bottom of dice 1-5}
		cout << "Do you wish to reroll the dice?" << endl;
	cin >> answer; 
	}

	system("PAUSE");
	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Same random numbers generated each time through

#2 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Same random numbers generated each time through

Posted 07 February 2013 - 01:36 AM

The random functions are set up to repeat their sequence - for testing purposes. If you want random random sequences, then you need to set the random number generator with a seed number, which changes each time.

srand(&time());



Add this line of code BEFORE anything else dealing with random numbers. Call it ONCE, not inside a loop. As long as your runs of the program are more than a second apart, you should be OK.

There is a LOT of details to working with random numbers - don't think that anything you can do on the computer will be TRULY random - because it won't. If you're not careful, it can be quite skewed in what it will produce for you.

Looking at your code, I'd say your call to srand() is not correct. I don't know what's right for C++, however.

This post has been edited by Adak: 07 February 2013 - 01:38 AM

Was This Post Helpful? 1
  • +
  • -

#3 jnaveenkumar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-February 13

Re: Same random numbers generated each time through

Posted 07 February 2013 - 02:23 AM

View PostSplinteredChaos, on 07 February 2013 - 01:13 AM, said:

I'm having some difficulties with the program. The problem is that each time the program is run (meaning within the while loop) the same numbers are generated. I'm not quite sure why. I've done some other Google searching and common issues were people using rand() more than once or inside of a loop, with not seeding the generator in a close second. the call for rand() is well before the loop and only done once. I've also used the standard method of seeding with the time(0). Not quite sure where I need to go to make each time through a different set of numbers be generated.

*side note while this is an assignment, the loop is not a requirement of the assignment, I just have to generate a random set of numbers (like in a dice game).

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

int main()
{
	srand(static_cast<unsigned int>(time(0)));
	
        string answer;
   do {
        int randomNumber = rand();
	

	
	int die1 =(randomNumber % 6) + 1;
	int die2 =(randomNumber % 3) + 3;
	int die3 =(randomNumber % 4) + 2;
	int die4 =(randomNumber % 5) + 1;
	int die5 =(randomNumber % 2) + 4;
	

	cout << " ***** " << "  " << " ***** " << "  " << " ***** " <<
		    "  "      <<  " ***** " << "  " << " ***** " << endl; //top of dice 1-5
	
	cout << " *   * " << "  " << " *   * " << "  " << " *   * " <<
		    "  "      <<" *   * " << "  " << " *   * " << endl;
	
	cout << " * " << die1 << " * " <</*die 1 */
		    "  "  << " * " << die2<< " * " /* die 2 */
		 << "  " << " * " << die3 << " * " <</* die 3 */
		    "  " << " * " << die4 << " * " <</* die 4 */ 
			"  " << " * " << die5 << " * " <</* die 5 */ endl;
	
	cout << " *   * " << "  " << " *   * " << "  " << " *   * " <<
		   "  "      << " *   * " << "  " << " *   * " << endl;
	
	cout << " ***** " << "  " << " ***** " << "  " << " ***** " <<
		    "  "      <<" ***** " << "  " << " ***** " << endl; //bottom of dice 1-5
	cout << "Do you wish to reroll the dice?" << endl;
	cin >> answer; 
     }while (answer =="y");
	
 system("PAUSE");

    return 0;

}



if you code your program this way ...
i think u cant face that problem of getting the same number.

This post has been edited by jimblumberg: 07 February 2013 - 07:46 AM
Reason for edit:: Fixed code and quote tags.

Was This Post Helpful? 0
  • +
  • -

#4 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Same random numbers generated each time through

Posted 07 February 2013 - 02:29 AM

Welcome to the forum, jnaveenkumar! ;)

If you highlight your code, and click on the [code] icon in the editor, it will show your code a LOT better.
Was This Post Helpful? 0
  • +
  • -

#5 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Same random numbers generated each time through

Posted 07 February 2013 - 02:43 AM

Good job jnaveenkumar :)

I think the srand(static_cast<unsigned int>(time(0))); did the trick.

Welcome to DIC...in future, please use the [ code ] tags for posting code.

Now, coming to the problem. I would like to change jnaveenkumar's code a bit.

I am using the rand() directly to assign the variables.

int die1 =(rand() % 6) + 1;
int die2 =(rand() % 6) + 1;
int die3 =(rand() % 6) + 1;
int die4 =(rand() % 6) + 1;
int die5 =(rand() % 6) + 1; 


One thing you need to note...for dice values it is always best to do % with 6 than any other number.

Also, never do % with 2 for a dice problem, as the only values that you will get are 1 and 2, after adding 1.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#6 SplinteredChaos  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 112
  • Joined: 15-September 11

Re: Same random numbers generated each time through

Posted 07 February 2013 - 10:53 AM

Ok, so by including the do/while loop and adjusting the locations as listed above it fixed the "same every time through" problem, but now each value is the same. That was the reason I had the

int die1 =(randomNumber % 6) + 1;
the way I did. Then I was getting a different value for each die. If how I had it was not the right way, how would I still retain the difference in values per die?
Was This Post Helpful? 0
  • +
  • -

#7 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Same random numbers generated each time through

Posted 07 February 2013 - 10:59 AM

Well, you need to have die1 till die5...not just die1 in the loop

int die1= (randomNumber %6) +1;
int die2= (randomNumber %6) +1;
.
.
int die5= (randomNumber %6) +1;


regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#8 SplinteredChaos  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 112
  • Joined: 15-September 11

Re: Same random numbers generated each time through

Posted 07 February 2013 - 11:54 AM

Unless I am grossly misunderstanding where the loop is, they are all inside of the loop. The code looks very close to what #3 jnaveenkumar posted above. The difference would be I made the
int die1= (randomNumber %6) +1; 
all the same for each die.
Was This Post Helpful? 0
  • +
  • -

#9 jnaveenkumar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-February 13

Re: Same random numbers generated each time through

Posted 08 February 2013 - 02:34 AM

hi SplinteredChaos ,


the thing is that if you

*HAVE DIFFERENT VALUES TO ALL THE DIES INDIVIDUALLY THEN IT WILL BE 1,2,3,4,5, OR 6 IN THE DIES AND YOU WILL BE MISSING ONE OUT OF THE 6 OUTCOMES.

* IF U ACCEPT THE RANDOM VALUES VALUES THEN THERE IS DEFINITELY A POSSIBILITY OF REPETITION OF AT LEAST ONE VALUE.

* IF YOU USE THESE FUNCTION
 rand() 
THE VALUE WILL BE A RANDOM ONE .
                        int die1= (randomNumber %6) +1;
                        int die2= (rand() %6) +1;
                        int die3= (rand() %6) +1;
                        
                        int die5= (rand()%6) +1;
                     



* IF YOU ASSIGN THAT TO A VARIABLE AND MANIPULATE YOU CAN GET THE NON REPEATING NUMBERS...
                        int die1= (randomNumber %6) +1;
                        int die2= (randomNumber %5) +1;
                        int die3= (randomNumber %3) +3;
                        int die4= (randomNumber %3) +2;
                        int die5= (randomNumber %4) +1;
.
               

ITS UPON YOUR IDEA TO GET YOUR OUT BASED ON YOUR PROGRAM.....
Was This Post Helpful? 0
  • +
  • -

#10 SplinteredChaos  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 112
  • Joined: 15-September 11

Re: Same random numbers generated each time through

Posted 08 February 2013 - 12:28 PM

In case it was missed, this is a dice simulator, as if you were rolling a set of 5 dice. This is meant to eventually go in to a Yahtzee game, which if you are unfamiliar with it you can find information about it here: http://en.wikipedia.org/wiki/Yahtzee

I am readily aware that there is the possibility of all the numbers being the same on a roll. That actually NEEDS to happen every so often for the purposes of the game (to obtain a Yahtzee is to get all 5 dice the same value). Where the problem lies is that every single roll always produces the same number for all five dice. Example
5 5 5 5 5
2 2 2 2 2
3 3 3 3 3

I can provide screen shots if you need them. I need it so that each die is given an independant random number from the other. Mathematically there will be repeats and there will be the chance that all end up the same, but it should not be a case where EVERY number is the same EVERY single instance.

Can you provide an example of what you mean by assigning it to a variable?
Was This Post Helpful? 0
  • +
  • -

#11 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Same random numbers generated each time through

Posted 08 February 2013 - 01:29 PM

How to make the dice all work independently is shown in post #9, by jnaveenkumar, right above your last post.

raghav.naganathan also told (and showed you), how to do this, in posts #5 and #7, above.

You REALLY need to go through a C or C++ programming tutorial.

This post has been edited by Adak: 08 February 2013 - 01:32 PM

Was This Post Helpful? 1
  • +
  • -

#12 SplinteredChaos  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 112
  • Joined: 15-September 11

Re: Same random numbers generated each time through

Posted 11 February 2013 - 11:47 AM

Just wanted to say thank you to those that helped with this problem. I did eventually get it figured out. Adak, you were right, the answer was right in front of me and I was merely overlooking it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1