Rand() functions returning same numbers - I have used srand()!

This is a discussion on Rand() functions returning same numbers - I have used srand()! within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm programming a game at school, and I have enemys spawning randomly according to the rand() function called each ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    2

    Rand() functions returning same numbers - I have used srand()!

    Hello, I'm programming a game at school, and I have enemys spawning randomly according to the rand() function called each time in the gameloop.

    The problem is, the rand function returns same numbers frequently. Here's a example of the output:

    r: 6015
    r: 13708
    r: 6015
    r: 6015
    r: 6015
    r: 6015
    r: 6015
    r: 13708
    r: 31495
    r: 29414
    r: 31944
    r: 6015
    r: 6015
    r: 6015
    r: 6015
    r: 13708
    r: 31495
    r: 29414
    r: 31944
    r: 27474
    r: 6422
    r: 2510


    as you can see the number 6015 is appearing quite frequently.

    The code is about this:

    Code:
    #include <time.h>
    
    int main(){
         srand(time(0));
         while(...){
               cout << rand() << endl;
               ...delay...
               }
    }
    I suppose there is more relevant information, but I don't know exactly what is important, and I can't post the whole program, so if you require more information, please let me know.

    Thanks!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    See the other thread on random numbers. If you still have a problem, please post complete code that shows the problem [most likely that will show us what you are doing wrong, because I doubt that rand() itself is broken].

    --
    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
    Registered User
    Join Date
    May 2009
    Posts
    2
    Problem solved, I accidently called srand() at another place

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Arpeggio54 View Post
    Problem solved, I accidently called srand() at another place
    That is exactly what I suspected.

    --
    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. Writing unique numbers to an array
    By yardy in forum C Programming
    Replies: 6
    Last Post: 12-27-2006, 09:15 PM
  2. generating random numbers using srand
    By skyrnnr87 in forum C Programming
    Replies: 5
    Last Post: 11-24-2006, 06:45 AM
  3. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 03:43 PM
  4. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 09:45 PM
  5. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM

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