Random Numbers :: C++

This is a discussion on Random Numbers :: C++ within the C++ Programming forums, part of the General Programming Boards category; Hi. I am experience recursive random number. It seems that only happens when rand() is in a loop. For example: ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    Random Numbers :: C++

    Hi.
    I am experience recursive random number. It seems that only happens when rand() is in a loop.

    For example:

    --------------------------------------
    srand(time(0));
    int tempNUM = 0;

    for (int i = 0; i < 1000; ++i)
    {
    tempNUM = 1 + rand() %9999999;
    cout << tempNUM + 1;
    }
    --------------------------------------

    In a program I am working on with a similar code to the one above, the program generates duplicates random numbers. It seems I have to *wait* one second clock until it generates a something new. Does that have something to do with time(0)?

    I would like something that can generate a completely random number almost instantaneously even when it is in a for loop like the one above.

    Thanks,
    Kuphryn

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    When I compile this program, it works fine. A different number every cout. I am using Dev-C++, though. What compiler are you using?

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    I am using VC++ 6.0.

    Kuphryn

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    Try using the function GetTickCount() in place of time(0). It should update pretty fast.

    //EDIT: be sure to include windows.h

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 09:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-12-2003, 12:03 AM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 10:14 PM

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