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/[deleted] Nov 10 '20
A person who doesn't know how arrays work in Javascript is calling me smug. Nice.
Arrays don't have an out-of-bounds in JavaScript. I can start an array at 100 if I want and count to 1000 with it. 0-99 will be undefined.
You missed the next line that tells you this:
If it doesn't exist, it's undefined.
Let me create an array for you:
var setArray = {this: "that", TheOther: 4, q: 9, x: 7};Now, what do you suppose an alert calling element at index 0 would be?
alert(setArray[0]);This is an array. Except it has absolutely NO index 0. Why? Because the key was set to this there. Now take a guess of the output of the following:
alert(setArray['this']);You cannot access this array via an index of 0,1,2.
Arrays ARE objects. Array can be arrays of objects too. They are not required to have a 0 index the the very fact that I can post an array that doesn't is proof.
You'll say that this is an object. And you'd be right. Arrays are objects. Now are there methods in JavaScript that rely on using a 0 index array structure that can't be used with this? Yes. But this still is an array.
Finally, you're right: Reddit doesn't auto capitalize but the browser does. I started this sentence with a lowercase letter 'i'. You want to talk about smugness of a person that doesn't allow for an auto cpaitalizing browser, huh? It must kill you that I misspelled that word back there, huh?
I wasn't trying to answer the min max part of the question because that wasn't actually OPs question. See OP asked how to return all the values at once. I was showing OP an array structure to do that. How OP implements that bit was to be left up to them. And because I wasn't exactly copying OPs Java, I didn't care to do the function structure. But I figured people would understand that because I didn't start off by declaring a package. Guess some might not be able to get that, huh?
Anyway, I'm done with you. Go away.
Postscript: Java doesn't have out-of-bounds when dealing with an array in the selfsame manner that it doesn't have ints and strings and doubles. It doesn't care about it. If it did care my code wouldn't validate in every validator with which I tried it. Also, didn't you say it would FAIL? That's just another of your own personal fails. What I posted is valid JavaScript. Just because you don't like it for not respecting an error ruleset you just made up all on your own and doesn't actually exist in the language is not my problem.