• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Jill Stemm
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Jill Stemm
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ever since I found this suit I've felt strange new needs. And a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic