Problem with random numbers

This is a discussion on Problem with random numbers within the C++ Programming forums, part of the General Programming Boards category; So I made a function that creates a random number between the desired numbers. It works out all fine but ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    don't you call by chance srand before every call of your function?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    21,461
    I think yaya needs to be eternally confuzzled by Prelude:
    Random Numbers Tutorial
    Using rand()
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,238
    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, 04: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, 07:46 PM

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