Thread: Promlems with Random Numbers

  1. #1
    Unregistered
    Guest

    Unhappy Promlems with Random Numbers

    I am new to programming C++, so I am trying to make a simple Slot Machine Program. Everything works fine, except the random numbers. Every time I run the program, I get the same three "random" numbers! Here is the code I am using for my random numbers:

    int x=rand()%21;
    int y=rand()%21;
    int z=rand()%21;

    The numbers I keep getting are 20, 8, and 13. The random numbers I want to get is the range of 1 to 20.

    Thank you all for your help!

  2. #2
    Unregistered
    Guest

    Arrow Oops

    It should be: Problems with Random Numbers

  3. #3
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Check the FAQ

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    It could also be "problem with the FAQ"

    You should only seed with srand() once.

  5. #5
    Unregistered
    Guest

    Ummm

    What do you mean seed only once?

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    A seed initialises a series of random numbers. If you are re-seeding with the same number, you will get the same series.

  7. #7
    Unregistered
    Guest

    Arrow Ok...

    So what should my code look like?

    The FAQ just confused me even more...

  8. #8
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Did you try any of the examples in the FAQ, using iostream the second one is -

    Code:
    #include <cstdlib> //for rand
    #include <iostream> //for cout
    #include <ctime>   //for time
    
    using namespace std;
    
    int main(void)
    {
        srand(time(NULL));
        cout << "A random number from 0 to 99: %d"<< rand() % 100;
        return 0;
    }
    What's confusing?

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    58
    ok not that i like the fact that you didnt check the FAQ to begin with, but this isnt really in the faq, it just takes a little thinking, here is a sample of what your code should look like, i commented almost everything to show you what is happening thoughout the program.

    Code:
    #include <time.h>
    #include <stdlib.h>
    #include <iostream.h>
    
    int main()
    {
    
    int i, j, k; //some variables
    srand(time(NULL)); //sets a random seed for a random seed. 
                               //this will allow us to get a random number 3 times
    k=rand() %1000; //sets a number between 0 and 1000. (used later)
    
    for(i=0; i<3; i++){ //simple for statment notice that 'k' is not included here
                            //although it could be, its included at the bottom. also 
                            //notice that k gets increased by one each time.
     srand(k);         //this sets the random seed for our numbers. sence 
                             //k it self is a random number, when the program is run
                            //more than once it will be changed every time and
                            //sence k gets higher by 1 each time, the seed is
                            //never the same.
    
    j=rand() % 100;  //now that we have our truly random seed we can
                          //make random numbers this will give a number between
                          //0-100.
    cout<<j<<endl;
    k++;                  //k gets increased by one for the next time its used for
                            //a random seed
    }
    }
    if k wernt a random number it self k would end up being 1,2, or 3 and spit out the same numbers. sence it got increased by 1 each time. sence k IS a random number and has a fairly high field (0-1000), it will be random number such as 592 then the seed would change to 1+ the random number making it 593 then add another 1 to it making it 594 and so on.
    --== www.NuclearWasteSite.com==--

  10. #10
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Jesus, is it any wonder people are confused. There's no reason to keep calling srand(). When a program is run more than once it'll be run at a different time, making your k redundant.

  11. #11
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Sorensen is right.

    Only call srand() once!

    Look at Sorensen's example, I couldn't have done better myself.

  12. #12
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    rkjd2 >> Thats what is called a kludge. A work around that technicaly works but it isn't a pretty solution.

  13. #13
    Registered User
    Join Date
    Aug 2001
    Posts
    58
    ok first he wasnt asking for a SINGLE random number. there for he needs 3 random numbers, if i calll srand one time, rand will spit out the same number each time (trust me i've done this before). if it's put in a loop it dosnt matter what the time variable is sence the time stays the same thoughout the entire program (its only inilized once). this is actually pretty simple solution and it works well.
    --== www.NuclearWasteSite.com==--

  14. #14
    Registered User
    Join Date
    Aug 2001
    Posts
    58
    besides u have to use a variable for srand otherwise you couldnt manipulate it. and if you used a variable with 1,2,3 as values, you would be calling the same seeds getting the same numbers. there for you must have a random seed to give k a random number.
    --== www.NuclearWasteSite.com==--

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 10:14 PM