Forums Register Login

10,000 random numbers between 1 and the users choosen number

+Pie Number of slices to send: Send
I need an actionPerformed method that will take the number the user enters in the JTextField and generate 10,000 random numbers between 1 and the users number then output min max and mean of the random numbers. here is what I have so far which isn't much.

+Pie Number of slices to send: Send
 

Jill Stemm wrote:I need an actionPerformed method that will take the number the user enters in the JTextField and generate 10,000 random numbers between 1 and the users number then output min max and mean of the random numbers. here is what I have so far which isn't much.


Right. Well first off, this problem has absolutely nothing to do with JTextField's or ActionListener's, so forget about them completely for the moment.

What if the problem was described like this:
I need a method that will take a number (n) and generate 10,000 random numbers between 1 and n, and return min, max, and mean of the random numbers generated.

Think about it like that, and forget all about your GUI for the moment. There'll be plenty of time to plug that in when you know it works.

HIH

Winston
+Pie Number of slices to send: Send
 

Jill Stemm wrote:


This line of code does not do what you think it does. The parameter to the constructor of class Random is the seed value. You should not pass 10000 here because you want to generate 10,000 random numbers. In fact, you should not pass anything at all, just do this:

To generate 10,000 random numbers, you need to write a loop that runs 10,000 times and gets a random number from 'ran' inside the loop.
+Pie Number of slices to send: Send
Okay thanks I had just taken the 10,000 out because I read it was the seed. Can you help me with how to make the loop? here is what it looks like now. Sorry hate sounding like a total noob but I am and I have sat and looked at this code so long that I have myself confused.

+Pie Number of slices to send: Send
 

Jill Stemm wrote:Can you help me with how to make the loop?


Do you not know how to do that? If that's the case, then I suggest you read the tutorials, because even if we gave you the code (which we don't like to do here), you may not understand it.

I would also strongly suggest (again) that you forget about your GUI and concentrate on the problem. If it was me, I'd start out with a method declaration; perhaps something like:and then work from there. And just to clarify, the double[] is an array of 3 double values that the method returns containing the min, max, and mean.

Winston
+Pie Number of slices to send: Send
Winston,
I wasn't working with GUI I have the applet built just put the random number object outside the action performed method so it doesn't generate new numbers on each click. Here is my full code and you will see why it looks like that. I was only giving you the small piece I needed help with . I have done all of the rest of it myself.

+Pie Number of slices to send: Send
 

Jill Stemm wrote:I wasn't working with GUI I have the applet built just put the random number object outside the action performed method so it doesn't generate new numbers on each click.


Yes, but you haven't separated out this problem (min, max and mean) from the rest of your code. You should be able to write a completely self-contained class (or method) that does exactly - and only - that function; and it has absolutely nothing to do with buttons or text fields or ActionListener's or anything else in your applet.

It takes a while, but learning how to separate logic (or problems) is one of the most important things you will ever learn as a programmer.

Winston
+Pie Number of slices to send: Send
A critical component of becoming a programmer is learning how to separate out various parts of your program into discrete parts. This is what Winston is talking about. for example, in your program, some of the distinct parts I see are:

1) Get input from a user.
2) generate a random number between 1 and some upper limit
3) do something 10,000 times
4) find the maximum number out of a bunch of numbers
5) find the minimum number out of a bunch of numbers
6) find the mean of a bunch of numbers.

so...look at #2: generate a random number between 1 and some upper limit. Note that there is NOTHING HERE that depends on HOW YOU GET THAT NUMBER. You could get it from the console. You could get it from a database, or a file, or you could generate it randomly, or you could base it off the time of day...etc. It doesn't matter how you got it, but assuming you have it, you need a method that will do it.

So, for starters...forget about doing ANYTHING else. Just focus on writing a method that will generate a random number between 1 and <your value>. for starters, you can just hard-code some limit - like 45. Write a bunch of code that you will eventually throw away to TEST that method. Make that method ROCK SOLID. Then try passing in different limits, making sure it handles things like negative numbers or whatever weird cases you think are necessary.

Once you are done writing that method, pick something else on that list, and go through the same process. Maybe you'll pick item #3 - do something multiple times. Note that it doesn't matter WHAT that is. You could print to the screen. You could ring a bell. You could generate a random number (hmm.....). And to begin, you may only want to do it 10 times. If you can do it 10 times, it should be easy enough to do it 30 times, or 500 times, or even 10,000 times (hmm....).

Then keep building off that. I never write more than 2-3 lines of code before I compile and test. the more often you do that, the easier it is to find problems.
Oh sure, it's a tiny ad, but under the right circumstances, it gets bigger.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2437 times.
Similar Threads
Need help with compound interest program please
Problem with FOR loop..Need Help ASAP...
Input Validation and my struggles with it
Validation Problems
How do i change this piece of code from a double to a stirng
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 09:41:38.