12
luaq
6y

I wrote a little script that generates random numbers until it reaches 420, my luckiest go was 17. What was yours?

Script: https://sharecodesnippet.com/406

Comments
  • 3
    What is the range? 0-999?
  • 2
    Also do you prevent hitting the same numbers twice?
  • 7
    @neriald The range is 69-666 and I tried to make it unbiased, so theoretically it could go on forever.
  • 2
    @irene Thanks for the new information, I did not know this.
  • 2
    @irene theoretically an algorithm can be written to intentionally avoid that number, based on knowing how that PRNG works. But it would require intent and lots of work. But it could go on forever if they really wanted.
  • 1
    anyone else who immediately has gauss distributions and expected values in his mind?
    no?
    okay...
  • 2
    Pigeonhole principal dictates that this will never go on forever.
  • 1
    @Teknas untrue. The pigeonhole priciple does not state that every number will he hit. Only that at least one number will be hit at least twice. It's possible a number will never be hit.
  • 0
    @deadPix3l assuming even distribution, the pigeonhole principal can be recursively applied to sub sections. Meaning every hole will eventually be hit.
  • 1
    @Teknas the pigeonhole hole priciple states that given n holes, you can prove that at least one hole will contain two pigeons given n+1 pigeons. They may all be shoved into one hole. There is no garentee that any hole will or will not contain a pidgeon.

    Given a range of 0-n, one number WILL definitely be hit twice at n+1 iterations. But any given number is not garenteed to be hit at all. Also "normal distrobution" doesn't apply to PRNG. it may be true with the more popular ones, but it's not a requirement.
  • 0
    @deadPix3l agreed, but we have an infinite supply, so we will eventually hit.
    Again, assuming even distribution.
  • 0
    @Teknas roll a die. Then again. And again. Now explain to me, what properties of the universe require that it ever lands a six. Now take into account that microscopic changes in the die may force the weight toward another number, or the side opposite of 6 may be slightly rounded preventing it from ever stopping on that face. There are no guarantees
  • 0
    If you roll a die thrice,
    You have a guarantee that it will land atleast twice before three or after three.
    (1,2,3) or (4,5,6)

    Now in subsequent rolls where the number is in the first set, apply the rule again.

    With infinite tries, you will end up with each hole being chosen.
    I guess we have to derive this as a proof or something in college. I don't remember the details.
  • 1
    @irene I never claimed it was likely. In fact it's absurdly unlikely. But it's not zero. @Teknas claimed it will definitely hit. I was only saying as completely unlikely as it is, the math suggests it's not impossible to never hit a number.
  • 0
    Behold the extended pigeonhole principal
  • 0
    @Teknas yes. So given 9 holes and 10 pigeons, that garenteed that at least one hole contains 2+ pigeons. It does not guarantee that hole 2 contains any pigeons.
  • 1
    Hmm... Maybe I should go to college again.
    Had fun doing this @deadPix3l
  • 0
    @irene I would argue that the following algorithm classifies as a PRNG:

    Start with a seed value with n bits set.
    Take the top 8 bits, convert to decimal.
    Shift the seed by that amount, rolling bits over to the beginning. More mangling. But maintain that only n bits are set. Return and treat as new seed.

    This is a terrible algorithm. But it is in fact an algorithm. And it's not really random. But it may appear random unless you spot the patern.. It's psuedo random.

    This will never generate a number with x bits set, unless x==n
  • 0
    @irene further mangling can be done. As long as the "n bits property" is maintained. Unless my math is wrong, a 64 bit int, with 6 bits set would be 64 choose 6 == 74974368 numbers.
  • 0
    @irene yes. And I quickly realized that mistake. And you are correct that it would only produce 64 distinct numbers. It was a bad implementation. But in later comments i specified that further mutations and shuffling would achieve my original intent.

    But yeah. The first version is only 64 numbers. My bad.
  • 0
    @irene it depends on what you consider valid. Although I believe we have reached an agreement. Linguistically, any algorithm that generates numbers via a psuedo random method is a PRNG. By valid I assume you mean "an algorithm that anyone would use or respect" then yes. Mine fails. But if you don't look under the hood, and don't modulus, and only look at the output numbers with the human eye in decimal (the bitwise patterns that make it bad will not be noticable in another base to most humans), you could naively believe it's decent when it isn't. Many people don't know which PRNG their language natively uses. So you could be using this terrible algorithm and not even know it. You probably aren't. The people who make those language decisions scrutinize PRNGs heavily.

    But it's not infeasible that the PRNG youre using has some weird property that prevents certain numbers from being generated.
  • 0
    @irene agreed. Modern PRNGs are really good. But I'm a pedantic asshole, and when you said "never" I couldn't resist a good internet argument of "well actually under these unreasonable and specific conditions, technically..."

    But yeah. I wouldn't sweat it much. I couldn't even find an example of a bad rng. I had to make one myself, and got it wrong the first time :)
Add Comment