Randomization

This is a discussion on Randomization within the C++ Programming forums, part of the General Programming Boards category; I created a slot machine program which runs ok, but every time I run the program i get the same ...

  1. #1
    blackmagic
    Guest

    Question Randomization

    I created a slot machine program which runs ok, but every time I run the program i get the same numbers randomized - could someone please tell me what i am doing wrong...by the way I am using a = 1+rand() %10 (is there a better way)

  2. #2
    Spanky devour89's Avatar
    Join Date
    Nov 2002
    Posts
    148
    Uhh that way will allways give you the same number when you run it!

    use this I used it in my game also:

    Code:
    int random(int low, int high) 
    {
    srand(time(NULL));
    return low + rand()%(high-low+1);
    }
    and then when you need a random number do:

    Code:
    random (5,15); //a random number between 5 and 15
    Hope that helps

    -Devouring One-
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

  3. #3
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    You can use srand(time(0));
    and don't forget to include <ctime>
    none...

  4. #4
    Spanky devour89's Avatar
    Join Date
    Nov 2002
    Posts
    148
    Ohh yeah i forgot to say that sorry...
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by devour89
    Code:
    int random(int low, int high) 
    {
       srand(time(NULL));
       return low + rand()%(high-low+1);
    }
    Eh, no! Only call srand(time(NULL)) once in your program. Not every time you retrieve a random number.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Spanky devour89's Avatar
    Join Date
    Nov 2002
    Posts
    148
    I said that. Read it again:



    and then when you need a random number do:
    Code:
    random (5,15); //a random number between 5 and 15

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by devour89
    I said that. Read it again:

    and then when you need a random number do:
    Code:
    random (5,15); //a random number between 5 and 15
    If you do this:
    Code:
    int main()
    {
       for(int i=0; i<5; i++)
       cout << Random(0, 10) << endl;
       return 0;
    }
    You call Random(...) 5 times, and thus calling srand() 5 times. Not good, you should only call srand() 1!!! time in your program.
    Remove it from the random function and place it in the beginning of your program instead.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    seeding the generator at every call of the random function leads to a repeating pattern.. that's why you don't want to do it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Randomization
    By ldb88 in forum C++ Programming
    Replies: 12
    Last Post: 06-21-2006, 01:36 AM
  2. Arrrays, Loops, and Randomization
    By myrddin in forum C Programming
    Replies: 4
    Last Post: 05-21-2003, 05:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21