Thread: Random Function

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    6

    Random Function

    I have a question regarding the random function.

    In the program I'm building I have a function where a random number is needed

    Code:
    randomnumber = rand() %3 + 1
    but I also have to repeat the function 3 times

    but when I repeat the function 3 times, the random number in all 3 cases is exact the same

    does anyone know how I can avoid this?

    ps: I'm using codeblocks and I included time.h for this function

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You may need to post your code - I would guess that you are calling srand() multiple times with the same (or very similar) value.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Besides what Mats said, have a look at this and this.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There is also the possibility that you are not using srand() at all, and it so happens that the first three numbers generated are the same modulo 3.
    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

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    6
    my problem seems to be solved, I initialised the srand() in a loop, and that seemed to be the problem

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by T1m View Post
    my problem seems to be solved, I initialised the srand() in a loop, and that seemed to be the problem
    Yes, that is definitely a bad thing to do - it is almost always wrong to call srand() more than once in a program - the only time it actually makes sense is when you actually WANT to repeat the same sequence of random numbers for some reason.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM