3 Replies - 21092 Views - Last Post: 01 November 2007 - 12:06 PM Rate Topic: -----

#1 jayhuang   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 11-October 06

random number generator

Post icon  Posted 10 November 2006 - 09:34 AM

I tried to generate random numbers range from 1-20. Also, no repeats are allowed. There should be a way to prevent getting repeated numbers other than storing them in an array and check back and forth.

My code now can generate random numbers ranged from 1-20. but repeats are still there. How do I randomize their orders? That must be a way to erase repeats.

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

using namespace std;

int main()
{
 srand(time(NULL));

 int random_integer;
 for(int index = 0; index < 20; index++){
   random_integer = (rand() % 20)+1;
   cout << random_integer << endl;
 }
} 


Is This A Good Question/Topic? 0
  • +

Replies To: random number generator

#2 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 253
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: random number generator

Posted 10 November 2006 - 09:41 AM

If the generator were not producing duplicates, they would not be random, would they? Random (or pseudo random, in this case) means that it could be any number in the range, whether or not it's been picked before. If you wish to exclude some numbers, then you are no longer looking for random numbers.

You may wish to look into using random_shuffle() instead:

http://www.cpprefere...om_shuffle.html
Was This Post Helpful? 0
  • +
  • -

#3 jayhuang   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 11-October 06

Re: random number generator

Posted 10 November 2006 - 05:25 PM

View PostAmadeus, on 10 Nov, 2006 - 09:41 AM, said:

If the generator were not producing duplicates, they would not be random, would they? Random (or pseudo random, in this case) means that it could be any number in the range, whether or not it's been picked before. If you wish to exclude some numbers, then you are no longer looking for random numbers.

You may wish to look into using random_shuffle() instead:

http://www.cpprefere...om_shuffle.html



Thanks man. I got it.
Was This Post Helpful? 0
  • +
  • -

#4 Vick7   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-November 07

Re: random number generator

Posted 01 November 2007 - 12:06 PM

How did you get random numbers not to repeat? Did you use random_shuffle? I can't get mine to work, please help! I'm trying to randomly generate numbers from 1 to 10 without repeating though. My code looks like this right now:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <time.h>
#include <algorithm>

using namespace std;




int main()
{
	

	string mystr;
	int guess, actual;
	 /* initialize random seed:*/ 
	srand ( time(NULL) );


	/* generate secret number: */
	actual = rand() % 10 + 1;


	do {
					cout << "Guess a number between 1 and 10: ";
		getline (cin, mystr);
		stringstream (mystr) >> guess;
	if (actual<guess) 
		cout << "The secret number is lower.\n\n";
	else if (actual>guess)
		cout << "The secret number is higher\n\n";
  } while (actual!=guess);
	

	if (actual==guess)
		cout << "Congratulations!\n\n";



	system("pause");
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1