r/Python • u/baranonen • Jul 22 '20
I Made This Randomly Generate 69420 - Generate random 5-digit numbers until 69420 is generated
76
u/DobleO Jul 22 '20 edited Jul 22 '20
Not an expert either but I feel this belongs in /rlearnpython
12
u/baranonen Jul 22 '20
There are mostly questions on that subreddit. "I Made This" type posts are usually posted here.
90
u/malicart Jul 22 '20
"I Made This" type posts are usually posted here.
Because nobody follows the rules.
25
u/SnowdenIsALegend Jul 22 '20 edited Jul 22 '20
Yeah I mean what's even the point of having r/madeinpython if we are continuing to post creations in here.
I feel that sub should be deleted and everything should be allowed to be posted in here with the correct flair instead. Centralisation of info is good imo, instead of having to follow a bunch of different subs which can easily be done with different flairs instead.
5
u/sneakpeekbot Jul 22 '20
Here's a sneak peek of /r/madeinpython using the top posts of all time!
#1: I made a program that takes a video of a lecture and extract clean whiteboard images frome it | 12 comments
#2: I've made a 3D scanner that's fully automated using Python script on Android phone! | 1 comment
#3: Low effort but I felt like sharing. I wrote a program that'll count the amount of times any given musical artist has used the n-word in their lyrics. | 8 commentsI'm a bot, beep boop | Downvote to remove | Contact ^^me | ^^Info | ^^Opt-out
7
u/waltteri Jul 22 '20
Ah yes, the sub with literally hundreds of subscribers!
Yeah, let’s just update the rules/flares of /r/python to better accommodate different types of submissions. There’s already a good subscriber base here, and I’m sure most won’t mind posts like this. Or if they do, they can downvote the posts.
12
u/SnowdenIsALegend Jul 22 '20
A suggestion to people who downvote "I Made This" posts: browse by flair instead.
People need to be encouraged to build more stuff in python, and downvoters don't help.
2
u/BonahSauceeeTV Jul 22 '20
Yes!!! I will say though, I think this is a Reddit problem in general. Anyone active in a community long enough eventually becomes the grumpy old man complaining about the new kids posts.
→ More replies (1)4
u/WadeEffingWilson Jul 22 '20
Couldn't agree more. Creating a whole sub for something that can be managed by assigning and enforcing flares is too hamfisted. I can understand wanting to keep the "how-to's" and "help, code broke" posts in another sub but LWIM posts would be a good fit for here.
3
u/waltteri Jul 22 '20
Absolutely agreed. I was actually about to propose browsing by flair in my comment, but went with downvoting instead, so people wouldn’t stop the conversation with arguments like ”but my Reddit app doesn’t support that!”
Legendary username btw
→ More replies (1)6
u/malicart Jul 22 '20
Bots in this sub upvote every garbage post that is literally a copy and paste of code from an example.
→ More replies (5)1
u/drbobb Jul 22 '20
While we're at it, why have subs on reddit at all? Let's just stuff all posts into one single bag, and have flairs to mark posts by topic. Then we allow users to subscribe to flairs... oops, we just did a lot of work to arrive back at what we already had.
0
Jul 22 '20
[deleted]
3
u/malicart Jul 22 '20
nobody really wants to look at that stuff
That is all, highlighting for correctness.
6
u/you-cant-twerk Jul 22 '20 edited Jul 22 '20
TBH Idk where else they expect you to post it. r/madeinpython is fucking dead. What more would this sub be if people didnt post their creations? New modules and updates - and thats it? Sounds boring as hell. Give people a reason to want to visit /r/learnpython when they visit /r/python. Let them see cool shit - not people arguing over what belongs on this sub. These rules need a rework.
2
u/sir_lurkzalot Jul 22 '20
This right here. The whole reason I found r/LearnPython was because of a cool project I saw here
→ More replies (2)4
4
u/HowYaGuysDoin Jul 22 '20
It does. There was a post a few weeks ago from someone who decided to print something else other than Hello World for their first project.
It was two lines of code. Both print statements. It was more appropriate for /r/pics
9
Jul 22 '20
Newbie here. Why do you use "sys.exit()" instead of just placing code under "if answer = 'y'" or using a prompt module?
7
u/baranonen Jul 22 '20
Great question, and I don't even know why. Now, I'm updating the code like you said.
1
u/SDSunDiego Jul 22 '20 edited Jul 23 '20
I was not able to see their code but "sys.exit()" is the code to properly complete (exit) your code. At least I believe that's what the python 3 documentation says.
edit: since i'm getting downvoted. sys.exit([arg]) Exit from Python - System-specific parameters and functions
16
u/stieterd Jul 22 '20
Isnt this basically a brute force attack?
31
43
u/malicart Jul 22 '20
It would be if something was being attacked, this is just a random number generator.
-5
u/stieterd Jul 22 '20
Yes but i was curious if a program like this could fall under the definition of a brute force attack. I always thought that for a brute force you had to first have for example aaaaaa and next aaaaab and then aaaaac. Like in a order. Using nested for-loops.
12
u/malicart Jul 22 '20
You do not need to have anything first for a brute force attack, you could be using a dictionary of commonly used passwords that you loop through and there is no requirement for order.
→ More replies (3)2
u/tectubedk Jul 22 '20
You could have 100 monkeys in your basement typing away on keyboards until it match the desired value, and it would still be considered brute force, but any form of order is random chance
1.0k
u/theHighFastball Jul 22 '20
Forty thousand years of evolution and we've barely even tapped the vastness of human potential.
74
u/acharyarupak391 Jul 22 '20
this sounds like sth i'd hear in a sci-fi documentary
53
11
4
u/prohtos Jul 23 '20
That's the best thing I've read this month. Take my upvote you beautiful bastard.
1
4
u/_szs Jul 22 '20 edited Jul 22 '20
40000 years of evolution? Actually 40000 years ago, the modern human appeared after a million years of evolution (or more, depending where you start to count).
150 years of evolution theory and people still don't get it m(
Edit: Wow. Downvotes mm(
→ More replies (3)
272
250
u/Gwenju31 Jul 22 '20
Now continue trying until you generate 69420 in 69420 tries
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
→ More replies (5)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.
→ More replies (1)9
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.
→ More replies (1)4
u/theanfieldlegend96 Jul 22 '20
However some people can perceive concepts way faster than others, doesnt that make them more intelligent?
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:
Run Tries Average 98 680 452 194 907 99 365 911 196 634 100 39 297 195 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!
→ More replies (1)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
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
7
u/Ketogamer Jul 22 '20
I decided to make that program and I'm currently running it now. It's now at 500 attempts and counting, I'll update when it finishes haha.
3
u/qingqunta Jul 22 '20
Shouldn't it be quite fast?
5
u/Ketogamer Jul 22 '20
No, the inner loop is what op made; generating random 5 digit numbers until I get 69420.
Then once I have 69420 I see how many times I went through that loop. If I didn't get it Im exactly 69420 attempts, I start over again.
13
u/m_domino Jul 22 '20
No, this can be made a lot more efficient: you can just cancel each attempt if it has not succeeded on the 69420th try and start a new loop instead of looping until you find a 69420.
2
4
u/Username_RANDINT Jul 22 '20
Make sure to not print every attempt to the console. Printing is slow.
6
u/Ketogamer Jul 22 '20
I will definitely remember that for more serious stuff.
But having the numbers fly by was the only possible fun to be had with this haha.
0
Jul 22 '20
[deleted]
5
u/Ketogamer Jul 22 '20
Yeah I've called it off. I can confirm it's VERY unlikely.
1
u/tcor15 Jul 23 '20
That is fantastic. It makes sense it is unlikely, but you're confirmation made me laugh. Thanks.
4
u/baranonen Jul 22 '20
Here's the Github link
4
Jul 22 '20
Randint(a, b)
Return random integer in range [a,b], including both end points. 😉
4
u/baranonen Jul 22 '20
Looks like I need to read the documentations more carefully next time :D Also, thanks for letting me know
18
Jul 22 '20
Don't do
import *. Bad practice. Import only what you need. Also F strings are nicer replacement to old methods.trial = 0 while (number := randint(10000, 99999)) != 69420: trial += 1 print(f"Done in {trial} tries")0
Jul 22 '20
[deleted]
1
Jul 22 '20 edited Jul 22 '20
I kept
numberfrom OP's code but I am not really using it. We can remove that.If we just want to know trials it takes, this monstrosity will work:
x = lambda acc: acc if randint(10,99) == 69 else x(acc+1); print(x(0))Python isn't fully functional language and because of huge stack required for executing this for 69420, most likely it will crash. Switch to Haskell.
( ͡ᵔ ͜ʖ ͡ᵔ )3
Jul 22 '20
Thanks, I recreated it in R :D
library(tictoc)
randgen <- function(value,min,max) {
tic()
RandVal <- NA
counter <- 0
while(is.na(RandVal) | RandVal != value) {
RandVal <- sample(seq(min,max),size = 1)
print(RandVal)
counter <- counter + 1
}
print(paste("Randomly generated", value, "from numbers between", min, "and", max, "in", counter ,"tries."))
toc()
}
randgen(69420, min = 10000, max = 99999)
180
u/Chevaboogaloo Jul 22 '20
Hello, yes I have optimized your code. See below:
print('69420')60
23
u/Zanoab Jul 22 '20
6
u/Antoinefdu Jul 22 '20
There is always a relevant xkcd
2
u/Hopefully__Helpful Jul 23 '20
I hope Randall Munroe's next project is making an xkcd generator to ensure that within a quickly expanding set of randomly generated xkcd style comics, there will be relevant ones for literally everything.
41
Jul 22 '20
Lmao, I recreated the code and it generated 69420 in 773682 tries. That's a lot
28
Jul 22 '20
UPDATE: I got 37 tries. I feel like not even OP got that small a number
13
0
Jul 24 '20
[deleted]
1
Jul 24 '20
I don't have my PC next to me now, but OP has provided the code to his project. It's somewhere in this comments section.
3
22
u/SuspiciousOmelet Jul 22 '20
The next step is to have this run until you can generate 69420 in 69420 tries.
10
u/acsad Jul 22 '20
I think you could use the carriage return character ("\r") and set the parameter end of print to "", so instead of printing each number in a new line, you just keep printing on the same line one over the other. The visual effect is cooler IMO.
print(f"\r{number}", end="")
4
u/IamGonnaChangeMyself Jul 23 '20
use carriage return character
Hey, Can please you explain how this works?
5
u/acsad Jul 23 '20
A carriage return character signalizes to the output system (in this case the terminal) to move the position of the cursor to the beginning of the line. It gets its name from old typewriters, in which you would use the carriage return lever to return the paper to the beginning of the line (although in this case it would also jump to the next line), so you could keep writing.
On Unix systems, the carriage return is represented by the "\r" symbol. When you use it in Python on the print function, you are asking for the cursor to return to the beginning of the line, so when the next print occurs, it overwrites the previous content of the line.
However, since the print function in python automatically executes a newline after printing the content (by appending a "\n" at the end of your string), you must explicitly set the parameter end to be equal to "", so it will append nothing. Trying to perform a carriage return after a newline does not work (just like in a typewriter, after you roll the paper down, there is no way to return to previous lines).
1
56
Jul 22 '20
Really? This is the shit that gets upvoted?
6
4
u/pvkooten Jul 23 '20
Thanks for posting this, ffs this reddit became shit. Why the f did this get gold.... I should leave this reddit.
0
5
u/KingofLoops Jul 22 '20
Seriously, this is like 10 lines of code.
5
u/Matthew4588 Jul 22 '20
10? I feel like that's a bit much.
2
u/KingofLoops Jul 22 '20
Ya, I started with 3, but felt like I'd get responses like this one, saying it's too few.
14
1
u/lexmozli Jul 22 '20
Can you post the code to this? I'm a newbie and I love to look over other people's code, trying to understand it and stuff.
1
3
u/momentslove Jul 22 '20
Reminds me of the Random Sort algorithm which scrambles an input array randomly until it’s sorted.
1
1
1
u/YXAndyYX Jul 22 '20
Seems to be deterministic and not random at all. It always needs 219729 tries. 😜
1
u/baranonen Jul 22 '20
Another person said this too and I have no idea why. The reason is probably something suitable for a Tom Scott video.
1
u/baranonen Jul 22 '20
Or, is 219729 a special number too and I just got wooshed
3
2
2
2
2
2
2
u/Fried_Catfishies Jul 23 '20
I’ve been learning to code for a few weeks now and found this post last night. It brought me so much pleasure to recreate your program this morning. Thanks for sharing!
2
2
0
u/Resolt Jul 22 '20
Now try and optimize it. I assume you print out after every try, but this actually slows down the program. Take time on the running time and see if you can make it go faster.
Hint: threads (many other options as well)
1
0
1
u/hungryplesiosaur Jul 22 '20
If you stop after 69420 tries there’s basically 50% chance you found 69420. Fun!
2
1
1
u/t3hcoolness I make things do stuff Jul 22 '20
Now the number it creates is a new level of random integer. :o
1
1
1
1
1
u/IamScuzzlebut Jul 22 '20
I used to make silly programs like this on my calculator. Draw random nr between 1 and 999.999.999 and let it run during a class. Record highest and lowest of this session, highest and lowest all time. Then at the end of class I'd be like "ah 889.765.881" that's pretty high!
1
1
1
u/badge Jul 22 '20
import random
random.seed(35135)
print(random.randint(10000, 99999))
print("Done in 1 try")1
1
u/Some_Maker Jul 22 '20
Keep generating numbers till the average of all numbers generated is 69420. And the next number is 365.
1
u/michalethor Jul 23 '20
Made this myself to see what the smallest number of tries I could get, got 3 after around 10 minutes of running. Seed: 1486755419091
1
1
u/__xor__ (self, other): Jul 23 '20
import random
random.seed(35135)
print(random.randint(10000, 99999))Or if you want to start at 1 (not just 5 digit numbers)
random.seed(140645)
print(random.randint(1, 99999)1
u/RedditF1shBlueF1sh Jul 23 '20
Your program is broken. I've been watching it for 4 minutes and it takes the same number of attempts every time
1
1
1
u/SteeleDynamics Compilers/Algorithms Jul 23 '20
Now you to see if you can generate 69420 in under 69420 tries!
1
1
1
1
1
1
1
u/Seawolf159 Jul 23 '20
I have never thought of that. That's kinda cool I guess. The amount of tries it takes could be your random number of choice!
1
1
Jul 23 '20
Can someone explain this to me? Not the program itself but how this person is calling the program. Is this a git function? I’m new
1
1
1
1
1
Jul 24 '20
Here is the source code:
from random import randint
while True:
x = randint(10_000, 99_999)
print(x)
if x==69420:
print("Gotcha!")
breakThe program is also available here.
1
1
1
1
1
u/icemansun Jul 22 '20
Well done. Personally, I’m a fan of 42069, but i applaud your programming prowess regardless.
1
1
1
0
u/Ariech Jul 22 '20
Hey! I'd like to recreate that code for learning. Could you tell me what should I look for if I want to display it in command window(i guess it's it). I only know how to run code in IDE
2
-2
0
0
0
-1
u/TriG-tbh Jul 22 '20
Nice
On a more serious note, you can make it much faster if you don't print every single generated number - though a single print seems instantaneous, hundreds of thousands will significantly increase the time it takes to get to 69240
-1
-3
0
0
0
0
0
0
u/hawk-bull Jul 22 '20
I believe this can be modelled by a geometric progression which would say that the expected number of times should be 900,000 before each success. I did last take stats in high school though...
Not sure why it’s significantly lower
0
0
0
0
-4
-3
37
u/memduhcagridemir Jul 22 '20
Great job putting the copyright statement on top. Otherwise, people would steal your work!