Random Function

This is a discussion on Random Function within the C Programming forums, part of the General Programming Boards category; I have a question regarding the random function. In the program I'm building I have a function where a random ...

  1. #1
    T1m
    T1m is offline
    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,528
    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
    21,935
    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.
    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

  5. #5
    T1m
    T1m is offline
    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, 12:49 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01: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, 06:42 PM

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