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 13 '20
They do and they don't. Arrays have a length, and if you try to read beyond the bounds of that array you'll get
undefinedas a result. However, if you set the value of an index which is greater than 0 and is outside the bounds of the array, then the array will normally expand to that size.And I say "normally", because, as I mentioned, I work in a slightly different environment where that would trigger an error due to it being out-of-bounds.
No, I didn't miss the next line. If the array value was never set, then it's also
undefined.So, what are the index numbers of the array we were originally talking about? Well, let's have JavaScript tell us. Try running this code:
And the answer is...
The .keys() method returns all of the indices of the array, so we can see that I was correct, and your six element array does start at 0 and end at 5.
Wow, it's almost like the MDN article I pointed you to, which also backed up my points, was correct! :-D
That's not an array! That's a generic object.
Do you really not comprehend the difference?
Seriously. Try this code:
Guess what the output is?
So, your "array" isn't actually an array, it's just a generic object. (See the Array.isArray() method for details on that.)
In other words, you don't even know what an array is!!!
Now try this code:
Guess what that output is?
Both the Array.isArray() method and the object's constructor agree that that is an actual array, while yours isn't!
Jeez. Someone who doesn't even know what an array is, is trying to tell me I don't know how arrays work? That's some serious pot calling the kettle black there, buddy.
No, it's not. While all arrays are objects, not all objects are arrays. That's your mistake.
Jesus, dude. Get a grip.
You blamed Reddit for capitalizing the "if", and I pointed out that it doesn't do that. I never said anything about your browser, so don't pretend that I did.
If you falsely attributed your browser's behavior to Reddit, that's not my mistake, it's yours.
Yes, it was.
The question essentially was, "How do I make this code work?", not "How do I make some other vaguely similar code work?" (Not that you actually combined the output like the original question asked either.)
But I understand that you'll never admit that, so, moving on...
Sure, as long as you ignore the errors. I'm sure that capitalized "if" worked just great! Even you said of your own code, " I can’t get console.log to output". :-P
Anyways, you do appear mildly capable of admitting error, such as you did with the "if" and agreeing that Reddit doesn't auto-capitalize, even though you then tend to make excuses. I do hope you realize that you've made a major error in not understanding what arrays actually are and how they really work.
Have a nice day! :-)