Random Number

This is a discussion on Random Number within the C++ Programming forums, part of the General Programming Boards category; I've already tried to generate random numbers this way: srand((unsigned) time(NULL)); variable = rand()%30; but if i tried getting multiple ...

  1. #1
    Unregistered
    Guest

    Question Random Number

    I've already tried to generate random numbers this way: srand((unsigned) time(NULL));
    variable = rand()%30;

    but if i tried getting multiple random numbers, I always got the same ones. Can someone give me a better method for generating a random number?

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    a better method
    Mmmmmm............
    Perhaps you're doing something wrong!

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Do a board search, there's a whole load of posts on this topic...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    That should work pretty good for random numbers, you could post more of your code so someone can check it.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's a quote from Prelude in another thread:
    >well im triing to get random numbers between say 23 and 35.
    r = ( rand() * ( 35 - 23 ) ) / RAND_MAX + 23;

    >if i use rand()%35 than it picks numbers between 0 and 35.
    Small ranges like that are frighteningly not random, so you'd best stick with the code I gave above. It uses the high order bits of a value instead of the low order bits which your method uses.

    -Prelude
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It could be that when they say "when I do it 10 times, it's the same", they really mean they're running the program 10 times and always getting the same number.

    In this case, you'll need to use 'srand' to seed your random number generator.

    If you'd have read the FAQ you'd know this.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >In this case, you'll need to use 'srand' to seed your random number generator.
    From the first post...
    I've already tried to generate random numbers this way: srand((unsigned) time(NULL));
    variable = rand()%30;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    quzah is saying that s/he needs to reseed before each call to rand(). This should work by the way.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by golfinguy4
    quzah is saying that s/he needs to reseed before each call to rand(). This should work by the way.
    I believe that would be incorrect.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Hammer
    >In this case, you'll need to use 'srand' to seed your random number generator.

    My bad. That's what happens when you post while talking on the phone...

    uzah is saying that s/he needs to reseed before each call to rand(). This should work by the way.
    No. This is not what I meant because this is incorrect.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Unregistered
    Guest
    Depending on what application you want to use your random number generator for, you might try downloading a better random number generator that I wrote. You can find it at:

    http://www.planetsourcecode.com/vb/s...=4385&lngWId=3

    This random number generator outputs random numbers with a perfectly uniform distribution. This might be bad for your purposes, but in combination with the modulus operator you should be able to manipulate the distribution a little.

    My other suggestion is that instead of seeding the traditional random number generator directly from the clock, you might try the stdlib.h function randomize(). I'm not sure exactly what that function does, but it works pretty well for me.

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Unregistered
    My other suggestion is that instead of seeding the traditional random number generator directly from the clock, you might try the stdlib.h function randomize(). I'm not sure exactly what that function does, but it works pretty well for me.
    Is randomize a standard function, or is it compiler specific... I can't see it in the standard reference sites?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is randomize a standard function, or is it compiler specific...
    In a word, Borland.

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Unregistered
    Guest
    so where do I put the srand() in my code?

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Unregistered
    so where do I put the srand() in my code?
    I take it someone has never read the FAQ? You need to call srand one time only, before you ever call rand. So basicly just call it right after you declare your variables in main. Or hell, since you're using C++, just do:
    Code:
    int main( )
    {
        srand( time(0) );
    
        ...my stuff here...
    
        return 0;
    }
    Since C++ doesn't care where you declare variables, just call srand first.

    Quzah.
    Hope is the first step on the road to disappointment.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 03:08 PM
  2. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 01:56 PM
  4. random number tutorial
    By 7stud in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2005, 03:41 PM
  5. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 08:34 PM

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