srand() question

This is a discussion on srand() question within the C++ Programming forums, part of the General Programming Boards category; i know what srand does and how to type it but instead of typing NULL can you use like a ...

  1. #1
    Self-Taught Noob
    Join Date
    Jan 2005
    Location
    Ohio
    Posts
    38

    srand() question

    i know what srand does and how to type it but instead of typing NULL can you use like a 1 so it creates a number every milisecond?

    typing it as follows:

    srand(time(1));

    or something like it.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    rand() creates a sequence of pseudo random numbers
    srand() just defines the start point for the sequence

    Code:
    srand(1);
    a=rand();
    b=rand();
    c=rand();
    srand(1);
    a=rand();
    b=rand();
    c=rand();
    The second set of 3 rands will always be the same as the first set
    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 Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    try
    Code:
    srand(GetTickCount());
    and include windows.h

    thats only on win32 machines
    Good Help Source all round
    Good help for win programmers
    you will probably find something in here
    this thing also helps

    if you have never tried any of the above then maybe you should, they help alot

  4. #4
    Self-Taught Noob
    Join Date
    Jan 2005
    Location
    Ohio
    Posts
    38
    what does srand(GetTickCount()); do?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    It means you should at least look up those two functions in the relevant manual pages.
    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.

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    As Salem has stated, if you seed your random number generator with a constant, you will get the same pseudorandom sequence each time. Initiallising it with GetTickCount() will ensure that it is different each time.

    GetTickCount() on a WIndows machine returns the number of milliseconds since the system was last started.

    *** EDIT ***

    Salems next post happened while Iwas typing, he is right of course, you do need to learn how to look up basic functions like these in the help, MSDN or other online resources.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Lightbulb Random number - Time delay - Loop

    Here’s what you have to do:
    1.
    Make random numbers.
    2. Get & compare elapsed time.
    3. Make a loop.


    Your program requires the use of time for two different purposes:
    1.
    To seed the random number generator. Ideally, you'd like to seed the random number generator with a random number!

    2. To keep track of elapsed time, so you can generate a new random number every millisecond.



    …so it creates a number every milisecond?
    Whenever you do something over-and-over, you need a loop!


    For time-delay you have a choice:
    1.
    You can run a “tight” loop re-checking the elapsed time at as fast as the CPU can go. (This makes your program a CPU hog.)

    2. Pause or Sleep() for a fixed time during the loop. All of the methods of pausing are system-specific. There is no way to do this using pure ANSI/ISO standard C++.

    You can use time() from the <ctime> header, or if you’re running Windows, use GetTickCount from the <windows> header. There is a problem with time() on Windows systems-- It doesn’t get updated every single millisecond… it "jumps"… Oh, I forget… every 40ms or so.

  8. #8
    Self-Taught Noob
    Join Date
    Jan 2005
    Location
    Ohio
    Posts
    38
    wow you sure do know how to confuse i kid.

    i fixed my program through a different way. thank you for your posts and maybe ill be able to learn more outta it next time.
    No one died when Clinton lied.

    Compiler: Borland C++ Builder
    OS: Windows XP

Popular pages Recent additions subscribe to a feed

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