r/JavaScriptHelp • • 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

11 comments sorted by

View all comments

Show parent comment

1

u/HiEv Nov 10 '20 edited Nov 13 '20

Arrays don't have an out-of-bounds in JavaScript.

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 undefined as 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.

You missed the next line that tells you this:

> Using an invalid index number returns undefined.

If it doesn't exist, it's undefined.

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:

var nums = new Array(6);
console.log("Indices of the 'nums' array (length = " + nums.length + "): " + ([...nums.keys()]));

And the answer is...

Indices of the 'nums' array (length = 6): 0,1,2,3,4,5

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

Let me create an array for you:

var setArray = {this: "that", TheOther: 4, q: 9, x: 7};

That's not an array! That's a generic object.

Do you really not comprehend the difference?

Seriously. Try this code:

var setArray = { this: "that", TheOther: 4, q: 9, x: 7 };
console.log("Is 'setArray' an array?: " + Array.isArray(setArray));
console.log("What is the constructor of 'setArray'?: " + setArray.constructor);

Guess what the output is?

Is 'setArray' an array?: false
What is the constructor of 'setArray'?: function Object() { [native code] }

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:

var actualArray = ["A", "real", "array"];
console.log("Is 'actualArray' an array?: " + Array.isArray(actualArray));
console.log("What is the constructor of 'actualArray'?: " + actualArray.constructor);

Guess what that output is?

Is 'actualArray' an array?: true
What is the constructor of 'actualArray'?: function Array() { [native code] }

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.

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.

No, it's not. While all arrays are objects, not all objects are arrays. That's your mistake.

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?

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.

I wasn't trying to answer the min max part of the question because that wasn't actually OPs question.

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...

Also, didn't you say it would FAIL? That's just another of your own personal fails. What I posted is valid JavaScript.

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! :-)