why does randomize function returns the same value every time?

This is a discussion on why does randomize function returns the same value every time? within the C++ Programming forums, part of the General Programming Boards category; hi. i have a bug problam. i was writing a program and used the rand() function and discovred that the ...

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

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,198
    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 09: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, 02: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, 09:18 AM

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