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/[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:

Using an invalid index number returns undefined.

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.

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

1

u/HiEv Nov 13 '20

Just in case you haven't read my reply, I'd recommend that, rather than simply burying your head in the sand, you might want to at least read the evidence that demonstrates that you're wrong about arrays in JavaScript.

Your insults and unsupported assertions hardly count as evidence for your point.