Thread: why does randomize function returns the same value every time?

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    3

    why does randomize function returns the same value every time?

    hi.
    i have a bug problam.
    i was writing a program and used the rand() function and discovred that the function returns the same value every time!
    here is the code:
    Code:
    int number = rand()%100;
    and i remembered to include the stdlib.h in the program...

    tnx for all the helpers...

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    Use srand() as well, as shown in the FAQ
    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.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    call this once and only once in your program:
    Code:
    srand(time(0));
    You will also need to include ctime for that to work.

    Edit: damn, beaten!

  4. #4
    the Wizard
    Join Date
    Aug 2004
    Posts
    109
    Because that the rand() always returns the same value after the start of the program..
    Try searcing the forum for something about random numbers. There's a lot of posts about it
    Last edited by MipZhaP; 01-29-2005 at 10:45 AM.
    -//Marc Poulsen -//MipZhaP

    He sat down, he programmed, he got an error...

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    3
    tnx all !
    that really helped me out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Function returns null value instead of zero
    By drdepoy in forum C Programming
    Replies: 3
    Last Post: 10-23-2005, 03:51 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM