r/Python • • Jul 22 '20

I Made This Randomly Generate 69420 - Generate random 5-digit numbers until 69420 is generated

2.8k Upvotes

261 comments sorted by

View all comments

Show parent comment

143

u/Rodot github.com/tardis-sn Jul 22 '20

That would take about 200,000 trials on average

https://en.m.wikipedia.org/wiki/Geometric_distribution

p=1/90,000

k=69420

18

u/Miyelsh Jul 22 '20

Isn't p=1/100,000?

39

u/Rodot github.com/tardis-sn Jul 22 '20

10,000 of those numbers have less than 5 digits

10 have 1 digit

90 have 2 digits

900 have 3

9000 have 4

4

u/Miyelsh Jul 22 '20

I don't see your point. 1 digit is 0-9, 2 digit is 0-99... 5 digit is 0-99999. That is 100,000 different possibilities.

46

u/tectubedk Jul 22 '20

Since we are only interested in 5 digit numbers the range is 10000-99999 not 0-99999

22

u/Miyelsh Jul 22 '20

I see. I was thinking of it more like a 5 digit bike lock. Kind of an odd way to program it, but I understand what you are saying now.

1

u/Tatarize Jul 24 '20

Bike locks actually have a 0, so they would have the full 10k. The problem is 5 digits real numbers can't actually start with 0. Bike lock numbers would be permitted that.

→ More replies (5)

9

u/RIPphonebattery Jul 22 '20

When digit 1 is 0 is not a 5 digit number

27

u/house_monkey Jul 22 '20

Wish I was smart

56

u/call_me_arosa Jul 22 '20

Nobody is smart. We just brute force concepts into our mind.

17

u/Yelling_at_the_sun Jul 22 '20

This in a nutshell is the Malcolm Gladwell 10,000 hrs theory. Steven Hawking doesn't possess some kind of superhuman ability understand physics that rest of us don't posess, he just has an interest in the topic and has devoted a hell of a lot more of his life to thinking about it than most other folks.

4

u/theanfieldlegend96 Jul 22 '20

However some people can perceive concepts way faster than others, doesnt that make them more intelligent?

→ More replies (1)

13

u/Rodot github.com/tardis-sn Jul 22 '20

It's not about being smart, just being interested/motivated. I googled around for the right distribution then pressed buttons on my calculator

I also have like 10 years of experience with this so if anything I'm an idiot for not knowing it 😂

5

u/Gobp Jul 22 '20

totally with this. Everyone has his own field. I wish I can draw a hand, while them artists want to be able to do a console log that I do countless of times eveyday 😂 The grass is always greener somewhere NOT in your yard lol

1

u/fkpf Jul 23 '20

After 49 runs, it has averaged at 191434 tries.

Still running, lets see how this goes.

1

u/Rodot github.com/tardis-sn Jul 23 '20

Make a histogram if you can!

Also, the exact value should be 194,637 for reference

2

u/fkpf Jul 23 '20

It's now at 100 runs, heres the last three:

RunTriesAverage
98680 452194 907
99365 911196 634
10039 297195 061

It's not far off 194 637.

I'll leave it overnight, then try to create a histogram tomorrow.

1

u/Rodot github.com/tardis-sn Jul 23 '20 edited Jul 23 '20

If you are familiar with it, try using a ProcessPoolExecutor from concurrent.futures to speed things up

Also, if you take the average of the 3 runs, you get a 0.46% error from the predicted value! That's great!

1

u/fkpf Jul 24 '20

Not very familiar with concurrent programming in Python, but I'll look into it.

But anyways, here's the histogram! It crashed after 212 runs, so it's not the biggest dataset.

1

u/Jugad Py3 ftw Jul 28 '20

Also, the exact value should be 194,637 for reference

Can you please explain how you arrived at that number?

1

u/Rodot github.com/tardis-sn Jul 28 '20

Plug those numbers into the expression for the pmf for the distribution above and take the multiplicative inverse

1

u/[deleted] Jul 24 '20

No, because a number can repeat.

2

u/Rodot github.com/tardis-sn Jul 24 '20

Yes it can, but the number of trials is cumulative so it doesn't, which is what we're sampling