Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 107,310 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 2,055 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



random number generator

 
Reply to this topicStart new topic

random number generator

jayhuang
post 10 Nov, 2006 - 09:34 AM
Post #1


New D.I.C Head

*
Joined: 11 Oct, 2006
Posts: 31


My Contributions


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.

CODE
#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;
}
}
User is offlineProfile CardPM

Go to the top of the page


Amadeus
post 10 Nov, 2006 - 09:41 AM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 11,738



Thanked 17 times

Dream Kudos: 25
My Contributions


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.cppreference.com/cppalgorithm/random_shuffle.html
User is online!Profile CardPM

Go to the top of the page

jayhuang
post 10 Nov, 2006 - 05:25 PM
Post #3


New D.I.C Head

*
Joined: 11 Oct, 2006
Posts: 31


My Contributions


QUOTE(Amadeus @ 10 Nov, 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.cppreference.com/cppalgorithm/random_shuffle.html



Thanks man. I got it.
User is offlineProfile CardPM

Go to the top of the page

Vick7
post 1 Nov, 2007 - 12:06 PM
Post #4


New D.I.C Head

*
Joined: 1 Nov, 2007
Posts: 21


My Contributions


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:
CODE

#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;
}
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 8/28/08 07:10AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month