Explain the logic behind rand() to me..

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 2074 Views - Last Post: 15 October 2015 - 06:47 AM

#16 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13567
  • View blog
  • Posts: 54,133
  • Joined: 12-June 08

Re: Explain the logic behind rand() to me..

Posted 12 October 2015 - 06:47 AM

Quote

Though, I guess I can see how you may want pseudo-random, semi-predictable randomness..?

Easy - testing. Something simple as regression testing between updates. Does this still do what it was reported to have done the last test? If I can pipe the same series of 'random' numbers through then I have a good idea on one facet.
Was This Post Helpful? 2
  • +
  • -

#17 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10735
  • View blog
  • Posts: 18,363
  • Joined: 19-March 11

Re: Explain the logic behind rand() to me..

Posted 12 October 2015 - 07:01 AM

View PostCansir, on 12 October 2015 - 04:02 AM, said:

My argument was meant to state my disagreement with rand() not already being seeded to something, like time(0).


In high-level languages, this is usually a convenience that's provided for you. C is a pioneer language, and a lot of the really basic libraries and routines were developed early in its history and nobody's ever really felt the need to make them nicer. If you wanted, you could easily write some wrappers around the existing random functions and make a "library" that works better for you.

Quote

Though, I guess I can see how you may want pseudo-random, semi-predictable randomness..?

Dude, writing that I couldn't help but crank my head slowly to the side like wuuuut.?


Yes, actually pseudo-randomness is actually much more useful than "real" randomness. Which is good, because it's really hard to get "really" random using a computer. In fact, it's impossible to get truly random behavior from a deterministic machine (though we can use hardware solutions to inject some nondeterministic elements - that's another question, though)

Why is pseudo-random good? Well, first of all consider what it means. A pseudo-random number generator G emits a stream of bits s[1], s[2], ... s[n] which has the property that an observer given only the output of the stream up to s[i] cannot predict the values of [si]+1, s[i+2], etc. However, an observer who knows some details of the initial state of G can know, with reasonable work, the value of any bit in the stream (simply by calculating it).

Okay, so consider a cryptographic application of this: I want to send you a message. I would like to arrange it so that you can read the message, but Ryano121 cannot. One way I can achieve this is by taking the xor of each bit in the message against the bits of a stream which is known to you, but not to Ryano121. Clearly, if I have a PRNG, and I can communicate to you the necessary details about its initial state, and conceal those details from Ryano121, then this purpose is achieved.

I think this is useful, since it's how I buy things over the internet. QED.
Was This Post Helpful? 1
  • +
  • -

#18 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 456
  • View blog
  • Posts: 1,179
  • Joined: 31-May 11

Re: Explain the logic behind rand() to me..

Posted 13 October 2015 - 03:50 AM

jon.kiparsky said:

and I can communicate to you the necessary details about its initial state, and conceal those details from Ryano121
Of course an interesting question pops up here - if my purpose is to securely communicate with you, and to do that I have to give you a "password" so that we're both on the same page - how do I securely communicate with you to give you the password in the first place, so that others can't use it to decode our following private communication? ; )

The catch is in the initial question, though - it's not necessarily the point to just securely communicate, but to do it in a particular manner with a particular level of security. It might be that to ensure a level of security we need to initially communicate in an entirely different way to "set it up".



Back on topic though - OP, like jon.kiparsky wrote, you have to remember that standard C++ library is in part built on/outright incorporates standard C library, and that - if only for historical reasons - isn't exactly the prettiest of things. Some languages have many a thing in their standard environments ready for you, some are a bit of a mess (PHP, I'm looking at you), some are more... "ascetic". What you're usually not told though - is that these fancy solutions are often build on top of these "barebones" functions. Not necessarily replacing them.


I can understand your frustration on some level - and hey, C++ can bring some serious mind benders in the language itself, you don't even have to look at the standard library. Take it as it is - C++ is a many-faced-god, bringer of both great pleasure and great misery. Be ready to embrace both. ; )
Was This Post Helpful? 1
  • +
  • -

#19 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 13,493
  • Joined: 08-August 08

Re: Explain the logic behind rand() to me..

Posted 13 October 2015 - 05:37 AM

View PostCansir, on 12 October 2015 - 05:02 AM, said:

My argument was meant to state my disagreement with rand() not already being seeded to something, like time(0).

But it is already seeded to something. You just need to reseed it if you want the random number stream to start in a different place. Sometimes, you don't! It's easier to test code when you know what the results should be, and if the random number stream is always the same, then it's easier to see if your latest code changes are working properly. When switching to "production" code, you probably want to use a random seed, hence the use of time().
Was This Post Helpful? 1
  • +
  • -

#20 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 792
  • View blog
  • Posts: 1,886
  • Joined: 24-April 12

Re: Explain the logic behind rand() to me..

Posted 14 October 2015 - 09:02 PM

So, pick a number that you haven't thought of. One that you've never conceived of. One that the world around you has had no influence on.


Sorry, reality is that there is no such thing as randomness. That's just the way the world is.

You have to basically come up with a number that is somewhere between difficult and impossible to predict. But difficult is much more likely than impossible.

At least using the microsecond at which you picked the number is unique to that microsecond. Perhaps you could use something that unique to come up with a random number?
Was This Post Helpful? 0
  • +
  • -

#21 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10735
  • View blog
  • Posts: 18,363
  • Joined: 19-March 11

Re: Explain the logic behind rand() to me..

Posted 14 October 2015 - 09:49 PM

View PostBBeck, on 14 October 2015 - 11:02 PM, said:

Sorry, reality is that there is no such thing as randomness. That's just the way the world is.


Hm. I'm pretty sure there are some physicists who would argue with this claim. :)

For my money, proving that the argument makes a difference would be hard enough. Sort of like free will - lots of people argue about whether we've "really" got it, but nobody can give a really good reason why it would matter.
Was This Post Helpful? 0
  • +
  • -

#22 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13567
  • View blog
  • Posts: 54,133
  • Joined: 12-June 08

Re: Explain the logic behind rand() to me..

Posted 15 October 2015 - 06:47 AM

I use my cubesat picking up background radiation to generate my random numbers... and to cook my popcorn.

Spoiler

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2