Thread: Problem with random numbers

  1. #1
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204

    Thumbs up Problem with random numbers

    So I made a function that creates a random number between the desired numbers. It works out all fine but when you call the function for a second time, the recieve the same number again and again. This is the function here:

    Code:
    int random(int LOW, int HIGH, int VALUE)
    {
        time_t seconds;
        time(&seconds);
        VALUE = rand() % (HIGH - LOW + 1) + LOW;
        return (VALUE);
    }
    So what am I doing wrong?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    don't you call by chance srand before every call of your function?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Quote Originally Posted by vart View Post
    don't you call by chance srand before every call of your function?
    Umm... I don't understand.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > time(&seconds);
    What happens after this?
    You're not even using seconds, so by bother reading the time, if you're not going to use the result?

    vart just took a guess that the line of code you omitted from your post is something like
    srand( seconds );

    Which, if you call the same function many times in the same second, will return the same result every time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    You should be able to drop the time bit completely out your random number generator. Heres one I made. Basically, like vart said, you only need to seed the randomization once. If you repeatedly seed it in a loop it will have the reverse effect.
    Code:
    int Rand(int lo, int hi){return lo+(rand() % (hi-lo+1));} 
    
    int main()
    {
          srand(time(NULL)); //Seed randomisation once at the start of the prog
          int i;
          for(i=1;i<=30;i++) cout << Rand(2, 10) << "\n"; 
          cin >> i;
          return 0;
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think yaya needs to be eternally confuzzled by Prelude:
    Random Numbers Tutorial
    Using rand()
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by yaya View Post
    So what am I doing wrong?
    You're calling time(). This makes me terribly suspicious that you are also calling srand() but you haven't posted it.

    You have a pointless argument called VALUE. It is needless, get rid of it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with random number generation
    By HAssan in forum C Programming
    Replies: 1
    Last Post: 03-27-2007, 05:49 PM
  2. Random Problem?
    By dizz in forum C++ Programming
    Replies: 4
    Last Post: 11-20-2002, 05:00 PM
  3. programming with random numbers
    By xstudent in forum C Programming
    Replies: 13
    Last Post: 05-21-2002, 01:36 AM
  4. random double numbers
    By Gugge in forum C Programming
    Replies: 7
    Last Post: 05-17-2002, 11:26 PM
  5. Promlems with Random Numbers
    By Unregistered in forum C++ Programming
    Replies: 13
    Last Post: 03-20-2002, 08:46 PM