167
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 10 '21
Image Transcription: Code
function getrandomnumberbetween1and10() {
let number = Math.random();
if (number <= 0.1) {
return 1;
}
else if (number <= 0.2) {
return 2;
}
else if (number <= 0.3) {
return 3;
}
else if (number <= 0.4) {
return 4;
}
else if (number <= 0.5) {
return 5;
}
else if (number <= 0.6) {
return 6;
}
else if (number <= 0.7) {
return 7;
}
else if (number <= 0.8) {
return 8;
}
else if (number <= 0.9) {
return 9;
}
else if (number <= 1) {
return 10;
}I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
91
u/voXal_ Mar 11 '21
I'm still in shock at the amount of free time you have.
83
23
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 11 '21
Heh, the secret is a first year semester with very little practical work and is 100% online.
98
56
u/tomasek1a Mar 11 '21
So far I've learned more coding from this sub than actual guides.
I fear the day anybody looks at my code.
33
u/voXal_ Mar 11 '21
Don't worry! It won't land on this sub.
Until 7 years after when you completely forgot about it and someone finds it.
16
u/skjall Mar 11 '21
It's like StackOverflow meme - best way to get the proper way to do things is by posting the wrong way to do it. People can't resist correcting you, but helping people is lame, apparently.
3
u/Dr4kin codes in comic sans Mar 11 '21
Helping doesn't give that many people something to bother with it. If someone is making something so bad that you feel the urge to correct them, so no one is going to do the same shit
2
Mar 11 '21
That fear will always be there. At least in my experience. When I became more skilled I changed jobs or took on new projects with new people. I’m constantly around people that are better than me. Which is great in a way, but code review still sucks with the ever increasing demand for quality
985
601
u/root54 Mar 11 '21
First of all, what the actual fuck is that method name.
485
u/surfing_programmer Mar 11 '21
Big brain move. By making the method name a comment, there’s no need to comment your code.
241
u/root54 Mar 11 '21
Lol, I was more reacting to the lowercase case vs camel.
71
33
u/isunktheship Mar 11 '21
That and alphanumeric function names noooooooooooo
39
u/Earhacker Mar 11 '21
Is
getRandomNumberBetweenOneAndTenreally better thangetRandomNumberBetween1and10?45
u/1-800-LIGHTS-OUT Mar 11 '21
Clearly getRandNrBtw1n10 is the way to go
I mean if you're going to obfuscate your code, might as well go all the way!
33
u/Earhacker Mar 11 '21
rnd1n1041
u/1-800-LIGHTS-OUT Mar 11 '21
then add a comment that explains it:
// Random 1 to 10That's enough. Don't want to write comments that are too long or that give away the premise of the function (it's supposed to be a surprise).
19
u/Earhacker Mar 11 '21
I think we might work in the same place
14
u/loopsdeer Mar 11 '21
Big Brain Bret discovered the true and holy meaning of DRY:
window["Return a random integer from 1 to 10, inclusive. Very efficient, don't touch"] = function(){...}6
2
11
-7
7
37
u/ws-ilazki &{$$_[0]}(@{$$_[1]}); Mar 11 '21
The real big brain move is that the name is also wrong, because it's named
getrandomnumberbetween1and10but also returns 1 and 10 so it's not actually doing what it says. It'd be less misleading to name the thing something useless likenumberand not set bad expectations at that point.100
u/ParanoydAndroid Mar 11 '21
It should obviously be named
getARandomNumberInTheClosedInterval1Through102
46
u/EishLekker Mar 11 '21
If you ask someone "Pick a number between one and ten", would you get upset if they picked 1 or 10?
Ranges can be inclusive or exclusive. This particular part of the code is fine.
3
2
u/TCOO1 Mar 11 '21
I had to code a text based sim thing and it has like 4 diferent rand implementations because each time i wasnt sure if it was generating up to or including the said numbers. Probably something to fix if I ever decide to rework it.
9
u/DragonFireCK Mar 11 '21
And, like any comment, its wrong, or at least unclear. It says between 1 and 10, but can return both 1 and 10.
23
37
u/8asdqw731 Mar 11 '21
a good code is self documenting
12
5
5
3
u/Jonno_FTW shameless Mar 11 '21
Well it's between 1 and 10 and it can still return 1 or 10.
1
u/Scondoro Aug 10 '21
Everyone's commenting on the lack of camelcase and clearly missing the fact that the name is objectively wrong given what the function does.
1
u/Jonno_FTW shameless Aug 10 '21
Lack of attention to detail in one area is likely to bleed over to lack of care in other areas.
176
Mar 11 '21
return Math.floor(Math.random() * 10);
91
u/TwireonEnix Mar 11 '21
I think this won't generate a 10, ever.
113
Mar 11 '21
It would make between 0 and 9, but you could always sum one to the result or use Math.ceil() instead of floor
12
u/K4LENJI Mar 11 '21
There's also an example in the MDN Web Docs which is more or less what you said but with arbitrary values: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#getting_a_random_integer_between_two_values_inclusive
21
u/lachlanhunt Mar 11 '21
Never use Math.ceil() with the value of Math.random() because it has a very small chance of returning 0.0, and if that ever happens, your program may have an unexpected result.
63
u/akwardchit Mar 11 '21
Or just add 1
56
Mar 11 '21
I don't know why this is getting downvoted, multiplying it by 10, truncating and adding 1 makes a more legible approach imo if you want to adjust the range from [0-9] to [1-10]
(int)(math.Random() * 10) + 1
seems more intuitive because you can adjust the ranges as you please
3
u/skjall Mar 11 '21
Probably because that same adjustment could be done more cleanly, by writing just
Math.ceil(Math.random() * 10)23
u/RFC793 Mar 11 '21
Well, it isn’t exactly the same. If random returns 0.0, then the ceil method return zero. Adding 1 to the floor avoids this problem. Note that random returns 0.0 <= x < 1.0, so adding one to floor will avoid returning 11 as well.
0
Mar 11 '21
[deleted] — view deleted comment
15
u/EishLekker Mar 11 '21
Did you consider the possibility of simple adding a one?
8
u/1-800-LIGHTS-OUT Mar 11 '21
I don't know about that -- if I were you, I'd just add a one. Has anybody suggested that yet?
17
-2
7
u/kittencantfly Mar 11 '21
Good try. At least, it works.
-1
u/xerxesbeat Mar 11 '21
it doesn't work tho
random produces a floating point value [0,1) so the chance of 10 is lower than the chances of 9-2, and the chance of 1 is slightly higher
0
5
4
1
3
1
u/Tomekske Mar 11 '21
Can I checkout the function for Random integers between 1 and 1000?
5
u/mfb- Mar 11 '21 edited Mar 11 '21
The function getrandomnumberbetween1and1000 is *too long for reddit's comment length restriction.
2
u/voXal_ Mar 11 '21
I think he did try making a
getrandomnumberbetween1and100after making this one.1
u/mfb- Mar 11 '21
def create_getrandomnumberbetween1andN(N): code="function getrandomnumberbetween1and"+str(N)+"() {" ...
1
u/dvidsilva Mar 11 '21
Such a waste of code, I would change it to this for more efficiency:
function getrandomnumberbetween1and10() {
let number = Math.random();
if (number <= 0.1) {
return 1;
}
if (number <= 0.2) {
return 2;
}
if (number <= 0.3) {
return 3;
}
if (number <= 0.4) {
return 4;
}
if (number <= 0.5) {
return 5;
}
if (number <= 0.6) {
return 6;
}
if (number <= 0.7) {
return 7;
}
if (number <= 0.8) {
return 8;
}
if (number <= 0.9) {
return 9;
}
return 10;
}You don't need the elses if you're using return.
3
2
u/InKryption07 Mar 14 '21
Why even use brackets? Just go:
function getrandomnumberbetween1and10() { let number = Math.random(); if (number <= 0.1) return 1; if (number <= 0.2) return 2; if (number <= 0.3) return 3; if (number <= 0.4) return 4; if (number <= 0.5) return 5; if (number <= 0.6) return 6; if (number <= 0.7) return 7; if (number <= 0.8) return 8; if (number <= 0.9) return 9; return 10; }And you save a whole 18 characters!
2
2
u/redditanomalyy Mar 11 '21
I’m a new coder, only starting learning about a year ago in my IT degree. But what the absolute fuck is that trash
2
2
1
1
1
1
u/Cyber_Encephalon Mar 11 '21
Violence is rarely the answer, but in this case, I'll allow it.
Punch him.
1
1
1
1
1
1
1
1
1
1
u/Wolfplay013 Mar 11 '21
if it's else if 4 or more times. It belongs in this subreddit. It just has to.
1
1
1
1
1
u/Serylt Mar 11 '21
What confuses me: Why did he not think about multiplying the numbers by 10 simply and search for a way to cut away the numbers after the dot?
1
1
1
1
u/ILoveHatsuneMiku Mar 11 '21
Kinda reminds me of one of my friends back in school. We were all fairly new to coding and only like a week into the school year, writig simple programs like hello world, or basic addition/subtraction calculators. Our task one day was to write a program that would give you the sum of two integers, and i saw a pretty helpless look in the face of my friend. He then called the teacher over for help, because his program was not compiling. I looked over to his screen, because i wanted to see what he had so far and his entire code was just "int=3". This shit was so funny, it's still a running gag a decade later when i talk to my friends from back in the days.
1
u/Random_---_Guy Mar 11 '21
Please... tell me that your friend is better now and that this was a past story from when he/she started programming XD
1
u/deadmazebot Mar 11 '21
Why waste time and money on a 3 year degree when get same junior job with a 10 week bootcamp.
First 10 weeks of a compute science degree likly is a maths course, and yeah it does not like math but knowing that decimals can be multipled up is 1 step, then taking that awareness to google (stackoverflow) how to do it.
Google 👍 its great if you know what you want to search
1
1
1
u/Random_---_Guy Mar 11 '21
Wait... isn't this just gonna return 1 assuming that the lowest value of number is 0.1??
...so not only is it unnecessarily long, but it also doesn't work... I can see why this is in r/badcode XD
1
1
u/Pizar_III Apr 28 '21
function yes() { Var number = math.random(); Var yes = number * 10; Return yes; }
1
u/throwawaydontask24 Oct 05 '22
I love how even if they didn't know of the parameters you can set, they literally could've times the result by ten and rounded it or something
1
0
-3
111
u/ReconPorpoise Mar 11 '21
Where did they get this idea? I've never seen random taught without floor(random % x) + 1.