time Delays and Random functions

This is a discussion on time Delays and Random functions within the C++ Programming forums, part of the General Programming Boards category; How do i write Time delay functions or instructions? Like Delay for 40us or 80ms? But how do i make ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    17

    time Delays and Random functions

    How do i write Time delay functions or instructions?

    Like Delay for 40us or 80ms?

    But how do i make them "overlap" if i had multiple time delays going on?

    How do u modify or manipulate the random function?

    Where can i get external random function librarys or librarys that have different random functions?

    Each programming language has different random functions that are different ways of getting a random generator how do i call different programming language librarys please?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by markphaser
    How do i write Time delay functions or instructions?
    depends on the operating system. *nix call sleep(), MS-Windows Sleep() (note difference in capatilization), don't know about MAC or other operating systems. See man pages or msdn for parameters.

    But how do i make them "overlap" if i had multiple time delays going on?
    There can be only one delay at any given time for the same thread. Afterall, you can't delay a delay.

    How do u modify or manipulate the random function?

    Where can i get external random function librarys or librarys that have different random functions?

    Each programming language has different random functions that are different ways of getting a random generator how do i call different programming language librarys please?
    In C and C++ rand() will generate a random number, and srand() will set the seed. Call srand() only once during the lifetime of the program (shortly after start of main function).. C/C++ probably will not be able to call random function from libraries compiled and built with other languages.

  3. #3
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Quote Originally Posted by Ancient Dragon
    *nix call sleep()
    Isn't it usleep()?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by OnionKnight
    Isn't it usleep()?
    I think *nix has both functions, the difference is the parameters, one is seconds and the other milliseconds.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    How do i set the "random seed" To get different random number sequences?

    What sets the random number sequences like how to i change the "order" of the "number sequence"?

    How do u change the current time at program's startup?

    Why is the sequence is different at each startup?

    How do u change the returns a random value in range?

  6. #6
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Code:
    #include <time.h>
    
    .
    .
    .
     srand (time(0));
    easiest way to seed rand, i think another way is to use the
    current process id but i havent ever used it so i cant comment.
    remember to seed rand only once in your program.

    EDIT:
    Your other questions are a bit unclear, but to generate a random
    number in a range 0 to x, call

    Code:
    number = rand ()/x+1;
    to set the interval between two numbers x and y, where x is
    the upper bound and y is lower, use

    Code:
    number = rand ()/((x-y)+1) + y;
    Last edited by Richie T; 02-19-2006 at 03:28 PM.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    What is current process id?

    set the interval between two numbers

    The interval u mean in time interval?


    ""Each seed, defines a different number sequence"".

    How many different seeds is there? how do u change the seed?

    How do u modify or manipulate the "seed" please?

    ""Initializing the seed to the time, make the number sequence different for each program session."""

    How do i initialize the seed to the time or to different times please?

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    how do i make a "Integer" Random function?

    how do i call system libraries that have random functions

  9. #9
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    rand() returns a random integer already, from 0 - RAND_MAX. To simulate a random 'float', divide rand() / RAND_MAX. Do you want some other PRNG other than what the CRT provides or something?

  10. #10
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    your questions are extremely incoherent. the code i posted with
    srand "seeds" the random function. it seeds it with the current
    time, measured in seconds from the epoch. this epoch is different
    on different systems, for example, on Unix it is the instant in time
    when the very first Unix system became operational. this is some
    date back in the 1970's, i dont know it by heart.

    basically, this time (0) returns the number of seconds from this
    epoch, and so it is a number that changes every second. this
    is why we use it to seed rand, and as you have already pointed
    out, each seed generates a unique set of random numbers.
    think of it as being a parameter to a function that does some form
    of calculation based on this parameter. it shouldn't be too hard to
    accept that different parameters give different results.

    with regards to process id, every program in memory has a unique
    identy that the processor uses. these are integer values, and
    since a processor operates so fast, it is acceptable to assume
    from a human point of view that the current process id in use
    is some "randomly" changing value (its not a random value, but
    because of the fact that there are a huge range of values, that
    change so rapidly, its a good seed).

    now what i meant by interval has nothing to do with time, i
    thought you wanted to generate an integer in a certain numerical
    range, such as 0 to 10 or 5 to 20.

    now i hope i've cleared some of that up for you, but i'm honestly
    not sure i have. you're questions dont really make much sense,
    (to me at least) so it is possible that i am misinterpretting them.
    try to be more specific about what you want and you'll get more
    specific answers. it appears that you seem to have some sort
    of confusion about the connections between rand and time, so
    i cant clarify this any more than i have.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    a random 'float', divide rand() / RAND_MAX.


    Do you want some other PRNG other than what the CRT provides or something?

    Yes where do i find them or how do i make them up please?
    I was thinking about calling other external librarys or system librarys from different programming languages like cobol, fortran,ect. because they have scientic and business random functions which C++ doesn't have or in the CRT

  12. #12
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    What is the theory behind random generators?

    How can i make a random generator by scratch and manually
    make one from ground up please?

    The Random function is controlled by a Timing clock?

  13. #13
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    basic theory from my understanding, without using some sort of specialty hardware is to come up with an algorithim, that based on some seed generates some new psudeo-random number, with a very even distrobution between the minimum and maximum for output, using that output as the seed for the next run of the algorithum. Many algorithims rely on nothing but a seed which can come from any where, clock, pid, something non-constant is probably best so that its not something you can guess and get the exact same results every time.

    Here is my question, why do you want to write one from scratch?
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  14. #14
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    Thanks for the help

    Where can u get different seeds?
    or can u make up seeds from scratch?

    The seed generates the psudeo random number?

    Which clocks?

    What is a "pid" please?

    ""something non-constant is probably best so that its not something you can guess and get the exact same results every time.""

    Can u please give examples of something non constant for a random function to use?

  15. #15
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Where can u get different seeds?
    or can u make up seeds from scratch?
    Answer:

    a seed ... can come from any where, clock, pid, something non-constant is probably best so that its not something you can guess and get the exact same results every time.
    >> The seed generates the psudeo random number?

    The seed that you provide.

    >> What is a "pid" please?

    "Process ID"

    >> Can u please give examples of something non constant for a random function to use?

    Code:
    srand (time(0));

Page 1 of 2 12 LastLast
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