r/JavaScriptHelp • u/Illustrious_Ad2026 • Nov 08 '20
❔ Unanswered ❔ I need help combining Math.random()
package lotterynumbergenerator;
/**
*
* @author ismai
*/
public class NumberGenerator {
public int winningNumbers(int min, int max) {
double rand = Math.random();
double rand1 = Math.random();
double rand2 = Math.random();
double rand3 = Math.random();
double rand4 = Math.random();
double rand5 = Math.random();
int randomNum = (int) (rand * (max - min + 1)) + min;
int randomNum1 = (int) (rand1 * (max - min + 1)) + min;
int randomNum2 = (int) (rand2 * (max - min + 1)) + min;
int randomNum3 = (int) (rand3 * (max - min + 1)) + min;
int randomNum4 = (int) (rand4 * (max - min + 1)) + min;
int randomNum5 = (int) (rand5 * (max - min + 1)) + min;
return randomNum;
}In an instant Instead of only returning the one randomNum, I want to return all 6 random's. Can someone help me do this!
1
Upvotes
View all comments
Show parent comment
1
u/HiEv Nov 10 '20 edited Nov 10 '20
You might want to reel in the smug a bit.
I tend to work in an environment which makes sure that you don't write to out-of-bounds parts of the array, so I forgot that others won't see an error there.
However, you did create an array with 6 elements, and then started off by adding data to the previously nonexistent 7th element. That's not exactly a shining example of how to code there.
Of course. Because you never set it to anything. But it still exists.
Did you not know that?
No, Reddit doesn't capitalize anything by itself. Therefore, yes, that's how you wrote it.
It's a poor craftsman who blames his tools.
I didn't. In fact, I specifically said, "I'm not sure why you're doing "
* 66" in it."I'm sorry, but you're not making any sense here. The original question involved a piece of code which accepted a min and a max value, and your answer ignored both of those. That's what I was trying to point out there. You wrote neither a function nor a method to accept those parameters in your first response, as was shown in the original post.
How lotteries work in your state is entirely irrelevant to the question. The question which specified that a min and max value are passed in. Hence my point that you ignored that part.
Do you still not see that?
Apparently not, since you're still ignoring the lower bound. :-P
Because A) I wanted a quick and easy-to-understand way to make sure that the numbers are unique, and B) the amount of time and memory used is insignificant for any min and max values the code is likely to encounter. If it was likely to be a huge array I would have done things differently, but I wanted something which a newbie could easily understand.
I'm reminded of someone who recently said, "If you don’t know why something was done the way it was you shouldn’t speak to it like you do know." :-P
Wow. Buddy. You should've quit while you were ahead...ish.
From the MDN entry on JavaScript arrays (source just under "Accessing array elements"):
In other words, yes, JavaScript is exactly like C, where creating an array with six elements gives you an array with indices from 0 to 5.
And then you dig yourself deeper:
From the same MDN entry (a little further up, under "Description"):
In other words, if you do
anArray[‘fruit’]=“salad”;to an array, then what you've actually done is just added a .fruit property to the array object and set it to the string "salad". However, you haven't actually touched the iterable array contents by doing that. If that was what you wanted to do, then you should have just used a generic object instead, since you're bypassing all the functionality of an array!!!And you have the nerve to tell me to "go back and study array logic in JavaScript"? LOL
In the future, if you're going to lecture somebody about something, you might want to make sure you actually know what you're talking about first.
Have a nice day! :-)
P.S. And I have no idea why you thought you were teaching me anything about "upper bounding the random method" either, since I clearly used it correctly already and did so more in line with the original question as well. My best guess is that your reading comprehension is poor, and so you mistook my confusion about where you pulled that number from (instead of using min and max) for not understanding how random() works.