an altenative PRNG seed (other than time(0))

This is a discussion on an altenative PRNG seed (other than time(0)) within the C++ Programming forums, part of the General Programming Boards category; okay, it's pretty standard practice to use Code: srand( static _cast < un signed int >(time(0))); to seed your PRNG ...

  1. #1
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787

    an altenative PRNG seed (other than time(0))

    okay, it's pretty standard practice to use
    Code:
    srand(static_cast<unsigned int>(time(0)));
    to seed your PRNG (ignore the downsides to rand() for the moment).

    but I was wondering if there was some other (portable) way to create another seed. for example, I have two programs, one a bubble sort and one a quicksort. when I run them like so:
    Code:
    jshao@MCP ~/Programming/C++/quickSort $ ./QuickSort && ../bubbleSort/BubbleSort
    the PRNG creates the same set of values in each program. now I know that's to be expected because they're using the same seed in that context, but I was wondering if there was something else I could use in case I wanted to force two programs that seed their generators at the same time to end up with a different data set (making the code for each program different isn't an option - the only difference I want in the code is the sort() method of their respective classes)

    basically, my question is this: I need to seed two generators (pretty much) at the same time and have them come up with different values, but the code for both needs to be the same (and it has to be portable, so /dev/random isn't acceptable), and I don't want to add any devices that create noise or anything of the sort...

    I'm not sure if this is even really possible, becuase the only way I can think is to introduce another "random" variable into the equation, but that's not possible (for obvious reasons), and I can't think of another non-random variable (like time(0)) that would suffice...

    edit: I know this isn't strictly C++, so if a mod feels there's a better place for this, please move it.
    Last edited by major_small; 08-02-2005 at 10:51 PM. Reason: word change and note.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    Pass the seed as a command line parameter, then do (crudely)
    srand(atoi(argv[1]));

    Code:
    ./QuickSort 1 && ../bubbleSort/BubbleSort 2
    There are all sorts of ways of generating different numeric strings in the shell
    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 Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Some sort of entropy pool instead of time(0)?

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Salem
    Pass the seed as a command line parameter, then do (crudely)
    hmm... kinda hackish, but it may unfortunately be the best way...
    Quote Originally Posted by Tonto
    Some sort of entropy pool instead of time(0)?
    thank you for summing up my question
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    There is no standard way of finding out information about whether other processes ran in the same second, so you always have the problem of time(NULL) returning the same result in two different processes

    I mean, you could do this if you're on a *ix platform.
    Code:
    srand(static_cast<unsigned int>(time(0) + getpid()) );
    Process ID's are unique amongst all active processes, and since all the low-numbered processes like 0 and 1 are permanent fixtures, the chance of time(0) + getpid() being the same in two processes is slim.

    Or you could do this
    Code:
    ./QuickSort && sleep 2 && ../bubbleSort/BubbleSort
    To make sure there is always a delay between the two, and thus ensure that time(0) will return different results.
    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
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I knew there was no way to make sure time(0) returned different times when two processes used it at the same time - I was just hoping there was some other thing I could use (kinda like the PID idea)... oh well... thanks for your help anyway...

    just an idea: would it be harmful to read some un-initialized memory?
    I'm working on that now, but the OS keeps giving both programs the same memory address...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Another idea: use the filesize of argv[0] in conjunction with time(0).

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    except both programs have the same exact code. it's only the classes they use that are different... well, they're off by a few bytes, now that I think about it (the class names and headers are different)

    I guess that could do, but I'd want something a little more... random than that... I thought about just creating a hash out of argv[0], but idk... it's just not satisfying
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #9
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    I thought about just creating a hash out of argv[0], but idk... it's just not satisfying
    I thought about that too, but that and my suggestion are flawed because both will fail might a user want to run the same program twice simultaneously.

    I'm convinced using the app's process ID is the best way to go. Clearly there is no portable function that is platform-dependent (mutual exclusivity afforded by definition), but you can use getpid() for Unix/Linux and GetCurrentProcessId() for Windows.

  10. #10
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    I ran into this same problem once in a programming contest, the testing program ran the contest program from a batch and because the program ran so fast, every 2 or 3 runs would have the same result.

    The solution is to use a higher resolution timer, I used a non-portable one, but a portable solution is to use clock() instead of time;

    the following code produce 2 separate values for each line, which I would assume would run milliseconds apart from each other.

    Code:
    #include <ctime>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	cout << clock () << endl;
    	cout << clock () << endl;
    
    }

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Have you tried time( 1 ) ?


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by quzah
    Have you tried time( 1 ) ?


    Quzah.
    I am guessing this is a joke, though I don't get it.

    but in case it isn't, the time parameter is output, not input, so time(1) will give an error.

  13. #13
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    doesn't clock() return the amount of time the program has been running? in that case, even if there was a delay in between the programs, they should still be pretty close (with the exceptions of a few cpu cycles maybe)
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  14. #14
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    According to cppreference clock() returns type clock_t which appears to be a typedef of type double as it also says that you can determine the amount of time in seconds by dividing the return value by CLOCKS_PER_SEC which apparently is defined by the OS somewhere and on POSIX compliant machines is defined as 1,000,000. So, if you want to cast type clock_t to type int so it can be used in srand() as a seed the difference between two successive calls in two successive lines of code will to small to make a difference. But, you could try multiplying by a factor of 10^7 and the differences even in just a line or two of code running may be enough to create different seeds in a random enough fashion to satisfy your requirements.
    You're only born perfect.

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    Yeah, but clock starts at 0, at program start, so testing it first is still going to produce much the same answer in both program instances.
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User defined seed value
    By blue5t1053 in forum C Programming
    Replies: 3
    Last Post: 04-01-2008, 07:22 PM
  2. Random Seed without Time function
    By Gabacus in forum C Programming
    Replies: 4
    Last Post: 03-14-2005, 01:13 AM
  3. Same seed for srand yields different results
    By codegirl in forum C++ Programming
    Replies: 3
    Last Post: 06-23-2003, 02:39 PM
  4. Religion
    By gnu-ehacks in forum A Brief History of Cprogramming.com
    Replies: 239
    Last Post: 01-26-2002, 09:44 AM

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