Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,219 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,416 people online right now. Registration is fast and FREE... Join Now!




random number generator

 
Reply to this topicStart new topic

random number generator

jayhuang
10 Nov, 2006 - 08: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
+Quote Post


Amadeus
RE: Random Number Generator
10 Nov, 2006 - 08:41 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,976



Thanked: 116 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 offlineProfile CardPM
+Quote Post

jayhuang
RE: Random Number Generator
10 Nov, 2006 - 04: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
+Quote Post

Vick7
RE: Random Number Generator
1 Nov, 2007 - 11:06 AM
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
+Quote Post

Reply to this topicStart new topic

Time is now: 7/4/09 08:31AM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month