CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 37
  1. #16
    Join Date
    Mar 2015
    Posts
    14

    Re: Seeding a C++11 random

    How about the hour value of the system clock?

  2. #17
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,552

    Re: Seeding a C++11 random

    Quote Originally Posted by kjsisco View Post
    How about the hour value of the system clock?
    Please read post #10
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.11.5)

  3. #18
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Seeding a C++11 random

    Quote Originally Posted by monarch_dodra View Post
    The issue with using *just* time is if you happen to seed two PRNG's at the same time (or in rapid succession), then you have high odds of seeding with the same number. I've seen this happen before, it's not pretty.
    Which I addressed in #10. If at all possible, avoid multiple separate PRNG's.
    if you require this, and you require a guaranteed non-dependancy between the PRNG's, then this is "difficult" to achieve in a guaranteed fashion.

    For example, in Java, when asking for a seeds are generated from a XOR of a global PRNG element and the current time.
    ANd I addressed this too "don't use a PRNG to seed other PRNG's"
    Doing so is just going to give you 'different' yet 'dependant' PRNG's you only need to reverse the first seed and you can regenerate all the others as well. XORing or other operations with constants won't break this dependancy.

    to make matters worse, afaik, java uses an equivalent of rand() to do the seeding, using a horrible PRNG to seed other PRNG's, just perpetuates the problem


    I could implement all of that of course (not very complicated), but mostly wanted to know if there was "1 liner goto recommendation" when you want to write a small program.
    yes... The simple solution... only use a single PRNG
    There are only a very small amount of "real" usage cases where you need different PRNG's.
    One case and this may sound counter intuitive is if you need to be able to recreate the same random number sequence over multiple program runs and this program extracts RN's on multiple threads.
    This assumes of course the same PRNG engine, if you use different engines with the same initial seed value, your PRNG's will be dependant but different.


    If you need the same PRNG-engine, but guaranteed different sequences and don't care about inter-dependancy. use time() for the first, time()+constant for the second, time()+constant*2 for the third etc. with constant sufficiently large (at least a few days).

    If you need to guarantee non-interdependancy. you'll need a REAL RNG source... -> use a webservice that provides one.

  4. #19
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Seeding a C++11 random

    Quote Originally Posted by 2kaud View Post
    This doesn't compile for me with MS VS 2013. error C3551: expected a trailing return type. Also shouldn't e() be eng()?
    FYI, typos aside, auto return type deduction is a c++14 feature ( and it's better than decltype()'ing, unless you know what you're doing ).

  5. #20
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,552

    Re: Seeding a C++11 random

    auto return type deduction is a c++14 feature ( and it's better than decltype()'ing, unless you know what you're doing ).
    Yes. Unfortunately VS 2013 doesn't support this - its a VS 2015 feature.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.11.5)

  6. #21
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,117

    Re: Seeding a C++11 random

    Some really great information in this thread. What can be the most challenging is actually understanding what the "randomness" is attempting to achieve. As an example, one element is security, and there are big problems if the sequence can be reverse engineered. Other times (such as many games) it is merely to provide a human with "different experiences" that are beyond their ability to predict and there may not be any need for randomness at all.

    When these are combined with the goal of writing the simplest code to achieve a desired result, many of the "don't do ...." can actually be perfectly acceptable solutions and/or the "this is the best way...." (to maximimize randomness, avoid dependencies, et.al.) may provide no value (or at least insufficient value to cover the costs of writing and maintaining the software).
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  7. #22
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Seeding a C++11 random

    Spot on...

    When people ask for "random" they can mean very different things with that...
    Throw a die 6 times even with an unloaded dice, you could end up with
    5, 5, 5, 5, 5, 5
    or
    1, 2, 3, 4, 5, 6

    so yes, those ARE sequences of values obtained from a random event.

    Show that sequence to people on the street however, ask if it they are random, and 99% will say "no".
    even though those 2 sequences are just as likely to occur in 6 rolls as any of the other 66 possible sequences.


    So when asked for "random" that can mean several things...

    "an unpredictable sequence of values" could be one of them.
    but
    "a predictable sequence of varying values"
    is equally possible, sometimes you want
    "a well distributed sequence of values in the desired range"
    etc...

    if you have more than 1 PRNG with separate seeding, then there's the additional issue of "what kind of dependance between the 2 PRNG's is acceptable".


    "random numbers" is a seemingly easy concept, but it really isn't. Suppose you make a game engine for a casino, in that case, even a minute imbalance in probabilities could cause you to draw an ace more often than statistically average, which could mean the casino looses out big over time.
    Now, think what could happen if it was possible to (after observation a while) predict the sequence of card draws if you used a lousy PRNG like C's rand() because you thought "it's good enough".


    know your PRNG's, know what you use them for, and know how distribution imbalances occur and how to fix them, and know how PRNG periodicity affects your outcome.

  8. #23
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,117

    Re: Seeding a C++11 random

    OReubens - All very good points, thanks for providing them. Being somewhat of a gambler, I ran a few simulations to test your hypothesis "a minute imbalance in probabilities could cause you to draw an ace more often than statistically average, which could mean the casino looses out big over time." My test case was an 8 deck shoe with 4 extra aces (a very significant 12.5% increase). I ran against automated engines for both Blackjack and Baccarat (I am not much of a Poker player), with the presumption that the (simulated) players were not aware of the increased ace's, and made no adjustment to their playing style.

    In both cases (fairly small sample, so there is not a "proof") the players winnings actually went down very slightly (about 2.3% for blackjack and 3.6% for Baccarat). Alas, I do not have the time to run a formal mathematical analysis....but thought this counterintuitive evidence would be interesting...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  9. #24
    Join Date
    Jul 2013
    Posts
    576

    Re: Seeding a C++11 random

    Quote Originally Posted by OReubens View Post
    know your PRNG's, know what you use them for, and know how distribution imbalances occur and how to fix them, and know how PRNG periodicity affects your outcome.
    You forgot to supply the references.

  10. #25
    Join Date
    Jul 2013
    Posts
    576

    Re: Seeding a C++11 random

    Quote Originally Posted by TheCPUWizard View Post
    I ran a few simulations
    You're missing the point.

    If the simulated situation doesn't fit the real situation then there will be a difference. It isn't harder than that.

  11. #26
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,117

    Re: Seeding a C++11 random

    Quote Originally Posted by razzle View Post
    You're missing the point.

    If the simulated situation doesn't fit the real situation then there will be a difference. It isn't harder than that.
    No, YOU missed the point....the statement was "the casino looses out big over time" with extra aces. This is a conclusion about the impact of the difference, which does not seem to be supported by evidence.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  12. #27
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Seeding a C++11 random

    Quote Originally Posted by OReubens View Post
    Show that sequence to people on the street however, ask if it they are random, and 99% will say "no".
    this is a bit unfair, probability and randomness are technical terms with many possible techincal meaning and formalizations, the choice of which is entirely conventional

    so, one should not expect the street man to "innately" adhere to the mainstream definitions used in science ( mainly the notion of probability as by Kolmogorov axiomatization and the idea of randomness as either physical (chaos,ergodicity,etc...) or epistemical ( a measure of ignorance ) ). We could speak for hours about all the possible ways of defining a quantity suitable to describe random phenomenas, and we could speak for hours about all possible ways in which randomness emerge, and in which sense something should qualify as "random" or not, and to what extent it should. And even then, we could not arrive to a definitive solution being many aspects of randomness still open.

    when confronted with expression like "randomness or likelyness of a sequence" most people does not think about "the probability of that configuration", and why should they ? ( most people simply does not KNOW what probability or a configuration are )

    if we really want a scientific concept analoguous to what that street man has in mind, we could better end up with something like entropy ( actually, it would be a rather bad analogy ) or even better a quantity more phsyically commisurable to that street man experience he relys on when trying to understand the question he's confronted to.
    For example, some people could think ( whether consciously or not ) to the *time* needed to obtain exactly those sequences by rolling a dice, compared to the time needed to obtain any sequence, and being that time of the order of days it makes perfectly sense that he considers those sequences "not random" at all. That is, his intuition ( modulo the meaning of technical terms he cannot be aware of ) is almost right in this case ( yes, if confronted to an higher entropy sequence he would probably says it's "random", but this is because he would consider sets of similar sequences in his mind, rather than that specific sequence, making his intuition again more or less correct ).

    that said, yes, I tototally agree on the fact that the mind of street-(and even not so street-)man is full of misconceptions about probability and randmness, but in more subtle ways.
    indeed, the dice problem above could be reformulated in a way to let a genuine misconception to emerge; for example, one could ask the street man to bet some money on a sequence, choosen among either (1,2,3,4,5,6) or, say, (3,5,1,2,2,4) and observe that most people would choose mostly the latter, for no rational reason.

    BTW, if probability is a slippery concept, money and probability ( or more generally, utility and probability ) and their inseparable cousins risk and time, are probably even more ...

    @ TheCPUWizard, Hi, happy to read you again here
    Last edited by superbonzo; March 14th, 2015 at 04:33 AM.

  13. #28
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Seeding a C++11 random

    you missed two important words
    which could mean the casino looses out big time.
    I didn't run actual analysis, I was making a point, which you missed

    Also I can now go... "you added 4 aces to the deck and make the assertion that this in some form is identical to a skewed PRNG" and I also wouldn't call adding 4 aces "a minute imbalance".

    Besides, the casino doesn't even have to loose, even if they benefit from a skewed PRNG, then this may cause them to be blamed/sued for rigging the games.

    The point being if you use a PRNG in a mission critical app, you better make sure your PRNG is behaving exactly according to spec and not "meeh,... it's close enough".

  14. #29
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Seeding a C++11 random

    Quote Originally Posted by superbonzo View Post
    BTW, if probability is a slippery concept
    more slippery than a teflon eel in a bucket of snot. :-)

    The thing to take home is that PRNG's are just that. The 'P' in there is important, it's what makes the PRNG predictable and repeatable. Even when randomly seeded, it's predictable and repeatable if you know the seed.
    So you typically hide the seed from the outside world to give the elusion of a real RNG, but in some usages, the nature of the PRNG gives noticable artefacts of it's 'pseudo' behaviour.

    The trick is to mitigate against that and there are various tricks to do that. A lot of devs don't know about them because "blabla boring non understandable math", I'll just use modulo X to get a 0..X-1 number and be done with it.

    I don't mind, it's how I make money, I get called in to fix those. :-D

    I tototally agree on the fact that the mind of street-(and even not so street-)man is full of misconceptions about probability and randmness
    and science, and politics, and ...

    Apparently, there's now a growing movement against those pesky ruthless chemists and food manufacturers putting that darned DNA in our food.
    I WANT MY FOOD TO BE DNA FREE DAMMIT ! (no I don't).
    http://io9.com/80-of-americans-suppo...ont-1680277802
    oh right.... Americans... ;-)

  15. #30
    Join Date
    Jul 2013
    Posts
    576

    Re: Seeding a C++11 random

    Quote Originally Posted by TheCPUWizard View Post
    No, YOU missed the point....the statement was "the casino looses out big over time" with extra aces. This is a conclusion about the impact of the difference, which does not seem to be supported by evidence.
    The discussion was pertaining to the properties of the random generation itself, not to changes in the probilistic experiment (the simulation).

    It's a big difference if a game simulation is unintentionally unfair because the random generator is imperfect, or if it is deliberately unfair because you decide to simulate the game with a loaded dice or bogus deck of cards.

    Consider the simulation you conducted. How do you know the result is because you added an ace and not because your random generator is fawlty? The challenge is how to ensure the generator doesn't skew the simulations, or at least to get a grip on to what extent the generator influences the simulations.
    Last edited by razzle; March 16th, 2015 at 02:43 AM.

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured