Calling srand() multiple times

Hello,

I understand it's a bad idea to call srand(with system timer or epoch time) more than once as it often will seed rand() with the same value even when the granularity is in milliseconds.

My question is, if I can guarantee a different seed every single time while the clockface is running, could I then do it?

What are the exact cons of calling srand(time.now()) if I can guarantee the call will only be made at certain time intervals thereby ensuring rand() is seeded with a unique value (an ever growing epoch time value) in perpetuity essentially.

thank you.

  • You should only need to call srand() once, when your app starts.

    That will change what you get back when calling rand after that.

  • yea I get that part

    theoretically and in an ideal world seeding rand just once with epoch is next to perfect for pseudo random number generation

    what I want to know is if I account for the flaw with (accidentally) reseeding with the same value, and I don't care about security or someone guessing the seed value then we're good to go right? 

    I could just keep calling srand(epoch) every x time intervals forever without ever affecting the quality of the randomness right? essentially cutting the deck forever right?

    thank you

  • TL;DR if you call srand multiple times, at best you don't get any additional randomness, and at worst you get less randomness.

    In general, you don't want to call srand() over and over again, only once at the start of your application (with a value that is as random as possible, of course.)

    What are the exact cons of calling srand time.now if I can guarantee the call will only be made at certain time intervals thereby ensuring rand is seeded with a unique value (an ever growing epoch time value) in perpetuity essentially.

    - calling srand multiple times doesn't make the output of rand any more "random"

    - in the worst case, calling srand multiple times makes output of rand less random (even if the seed is different every time). This is possible even if the exact values of the seeds aren't known. For example, if you call srand (with the time as a seed) at regular intervals (say, exactly once every 5 seconds), the pattern of numbers returned by rand could be obvious.

    See the answers here: stackoverflow.com/.../how-does-calling-srand-more-than-once-affect-the-quality-of-randomness.

    So now you would need a way to make sure the interval between successive srand calls is unpredictable aka random, which means you'd need another source of randomness.... I think you see where this is going.

    - additionally, rand is assumed to return a sequence of numbers with certain properties (e.g. uniform distribution - which means all numbers are equally likely), but that assumption will likely fail if you call srand over and over again


    Sorry for the weird formatting of this post. The forum is currently returning a lot of errors when I try to post parens and links. For anyone who's having trouble posting or editing:

    - try to avoid posting anything that looks like code lol. Certain usages of parens seem to be triggering forum errors (even surrounding multiple paragraphs with parens.)

    - don't post formatted links. this means that if you're editing an existing post with a link, you have to replace it with a plain text url, which will be converted to a link when you submit your post.

    - don't use the quote feature (which generates a link). manually copy and paste the text you want to quote and (if you want), use the quote format instead

  • > textbox just randomly disappears

    Oh sorry about that - it's a longstanding known issue where your reply will be cancelled if the post you're replying to is edited. Doesn't seem like it'll ever be fixed.

    Usually your reply will be saved and you can pick up where you left off if you press Reply again. In some rare cases you may lose some of your reply, especially if you typed/pasted a lot of text.

    The frequent forum errors - orange box with "an error occurred..." - seem to be new for today tho.

    Funny thing is that if I create a new post it doesn't have any of the problems I see in this thread. I can type parens, post links, etc. with no problems.

  • > my goal with calling srand epoch every x intervals would be to bolster or safeguard an edge case when the clockface calls rand outside of onUpdate i.e. after a crash  - global initialization of a variable - or when onHide is freeing up resources. 

    Sorry if I'm missing something, but what's the edge case here exactly? Are you worried that somehow rand will be called without srand being called first, for a given run of your app?

    If you call srand - with the current time as a seed - in your app's onStart function, then it should be guaranteed to be called before rand - obviously assuming that you don't purposely call rand first - and it will be good enough to generate a unique sequence of pseudorandom numbers for every run of the app.

    EDIT: wow I haven't hated the forums this much in years. I had to edit this comment roughly 2 million times before it was accepted

  • > Are you worried that somehow rand will be called without srand being called first, for a given run of your app?

    yes, that's exactly what happens!

    I have a global arr[] and I initialize another global variable by randomly picking an element from that arr

    I can't seed outside a function so there's a technical edge case where the very first call is an unseeded rand 

    I also manipulate the global variable during onHide at the user's request outside of onUpdate

    the earliest I can srand is at initialize()

    reseeding would happen inside onUpdate almost exclusively to take care of these two edge cases as I cannot implement rejection sampling outside a function where I'm initializing variables

    edit: I'd share my code here but I don't want to lose my mind

  • So why can't you call srand in your app's onStart? There must be some place you can call srand that's guaranteed to be run before rand.

    You could even wrap srand and rand in the same function while ensuring srand is only called once.

    https://pastebin.com/ERSsb0ti

  • ohh ok, didn't know that was an option

    so you're saying

    move srand( e p o c h ) from View.mc to App.mc at function onStart

    that would ensure the initialization of the global variable in View.mc is done with an already seeded rand
    right?