Win a copy of Terraform in Action this week in the Cloud forum!
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:
• Tim Cooke
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• Rob Spoor
• Bear Bibeault
Saloon Keepers:
• Jesse Silverman
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Carey Brown
Bartenders:
• Piet Souris
• Al Hobbs
• salvin francis

# Random sequence

Greenhorn
Posts: 11
1
• Number of slices to send:
Optional 'thank-you' note:
Hi again,

The plan was to write a code that lists 100 numbers (1-100) in a row....and then automatically:

-- Sum them up (number + number....)
-- Count the numbers (100)
-- Pick out the lowest number
-- Pick out the highest number

Everything works just fine but now I want to do the same thing with randomly picked numbers (100 out of 500). Here's what I have:

I would like to use this random generator to the job but I can't interconnect the two codes:

Thanks for any advice.

Master Rancher
Posts: 4510
38
• 1
• Number of slices to send:
Optional 'thank-you' note:

Marshal
Posts: 26915
82
• 1
• Number of slices to send:
Optional 'thank-you' note:
Basically you would replace this:

with a different loop which processed each element of your Set. You would have to make the appropriate change inside the loop as well.

Marshal
Posts: 74393
335
• 1
• Number of slices to send:
Optional 'thank-you' note:

Francis Oswald wrote:. . . lists 100 numbers (1-100) in a row. . . .

So why are you using a set? Have you found out what sort of data structure a Set is. What's more, you can't iterate a set with an ordinary for loop.
Use this:-In both cases you are creating an IntStream, which processes the numbers from the Random object. In one case you collect the numbers diretly into an array. In the other case, you have to turn the ints into objects, and you create a new Stream<Integer> with this method. Then you use collect(), and you can find the Collectors class has a method supplying a Collector to create a List. You can use the streams API to calculate your figures directly, but you need to practise writing loops, it would appear.

Bartender
Posts: 4709
183
• Number of slices to send:
Optional 'thank-you' note:

Campbell wrote: So why are you using a set?

I guess to ensure 100 distinct random values. How would you change your stream-examples to get the same result?

Campbell Ritchie
Marshal
Posts: 74393
335
• Number of slices to send:
Optional 'thank-you' note:
If they are distinct values, then they aren't random. That would be called random selection from a diminishing population, but it is different from real randomness. I could easily mimic that with a Stream, adding lines 4‑5:-...but it is easier to create a List 0...99 and shuffle it.

Piet Souris
Bartender
Posts: 4709
183
• 1
• Number of slices to send:
Optional 'thank-you' note:
It is random, it is just like a big lotto, drawing 100 balls out of 500 (drawing without replacement).

@OP
you have a TreeSet with 100 different values. Since the TreeSet uses natural ordering, the smallest value is the first element, the largest value is the last element. Have a look at the API of a TreeSet, and see how you can get the first and last elements.

And to determine the sum: you can use: for (int element: treeset) ...

I have to use this format, because for some reasons I get an error when using normal font (CSFR error, or something like that)

Bartender
Posts: 2900
150
• Number of slices to send:
Optional 'thank-you' note:

Campbell Ritchie wrote:...but it is easier to create a List 0...99 and shuffle it.

I too think shuffle is a better approach.

Small nitpick: I think OP wants 1-100, both your examples take 0-99. It's a minor change, but I hope OP can figure that one out.

Piet Souris
Bartender
Posts: 4709
183
• Number of slices to send:
Optional 'thank-you' note:
If OP wanted the sequence 0-99 (or 1-100) in some order, then I don't think OP would have much trouble determining min, max and sum. You guys are missing OP's last code snippet.

Campbell Ritchie
Marshal
Posts: 74393
335
• Number of slices to send:
Optional 'thank-you' note:
Agree, Piet.

Francis Oswald
Greenhorn
Posts: 11
1
• Number of slices to send:
Optional 'thank-you' note:
Thanks for the input so far but......why is this not working now? I'm having a really hard time understanding loops:

Master Rancher
Posts: 4510
38
• Number of slices to send:
Optional 'thank-you' note:

why is this not working

Please explain.  What does the code do now?  What do you want it to do differently?

Francis Oswald
Greenhorn
Posts: 11
1
• Number of slices to send:
Optional 'thank-you' note:

Norm Radder wrote:Please explain.  What does the code do now?  What do you want it to do differently?

Well it does nothing. I get error messages because of variable randomNumber. I want this thing to generate 100 out of 500 (500 max value) random numbers and summarize them and count them and also pick out the lowest and the highest number (from the pool of randomly generated numbers)....all in one go.

Master Rancher
Posts: 4510
38
• Number of slices to send:
Optional 'thank-you' note:

I get error messages

Can you copy the full text of the messages and paste it here so we can see them?

Campbell Ritchie
Marshal
Posts: 74393
335
• Number of slices to send:
Optional 'thank-you' note:
Don't mix println() and printf() if you can help it. Put a %n at the end of your format Strings instead.

Francis Oswald
Greenhorn
Posts: 11
1
• Number of slices to send:
Optional 'thank-you' note:
I've tried a new approach but now I get the error "the method min (int, int) in the type math is not applicable for the arguments (int, int[])". It wants me to change type randomint to int....which would destroy my whole concept again. Any ideas?

Francis Oswald
Greenhorn
Posts: 11
1
• Number of slices to send:
Optional 'thank-you' note:
Here's the code:

Campbell Ritchie
Marshal
Posts: 74393
335
• Number of slices to send:
Optional 'thank-you' note:

Francis Oswald wrote:Here's the code: . . .

Please show us what you are actually running. There are at least three compile time errors in that code.

Francis Oswald
Greenhorn
Posts: 11
1
• Number of slices to send:
Optional 'thank-you' note:
I know this code is a big mess but here's the complete thing. The new errors I'm getting are: "the method min (int, int) in the type math is not applicable for the arguments (int, Random)" and "count can not be resolved to a variable".

Campbell Ritchie
Marshal
Posts: 74393
335
• Number of slices to send:
Optional 'thank-you' note:
Yes, the min/max methods take two numbers of the same type. And you have forgotten to declare count.

Master Rancher
Posts: 4510
38
• Number of slices to send:
Optional 'thank-you' note:

"count can not be resolved to a variable"

Look at the scope of count.  The usage and declaration of a variable must be within the same pair of {}s. The extraneous {}s on lines 16 to 32 put count's declaration out of scope to refer to count on line 34.

Francis Oswald
Greenhorn
Posts: 11
1
• Number of slices to send:
Optional 'thank-you' note:
It's done. i did it with Arrays and it's working now. Mission completed.

Campbell Ritchie
Marshal
Posts: 74393
335
• Number of slices to send:
Optional 'thank-you' note:
I missed count there. I can see another pair of {} in the wrong place, and the compiler won't notice. Please show us the working code.

Saloon Keeper
Posts: 8779
71
• Number of slices to send:
Optional 'thank-you' note:
Congratulations. Please post your working code so that others may learn from it.

Francis Oswald
Greenhorn
Posts: 11
1
• Number of slices to send:
Optional 'thank-you' note:
Working code:

Campbell Ritchie
Marshal
Posts: 74393
335
• Number of slices to send:
Optional 'thank-you' note:
Please indent your code correctly. You can cause yourself no end of confusion because you haven't indented the loops.
Consider using a for‑each loop rather than a for loop to iterate the arrays, but remember a for‑each loop works on a “read only” basis.
Don't print the individual array elements. Try this instead.
Let's see if we can't write Streams to create that array and obviate the need for a loop:-Why are you using an array of arrays rather than an ordinary array?

Piet Souris
Bartender
Posts: 4709
183
• Number of slices to send:
Optional 'thank-you' note:
@OP

in your opening post, you ended with a nice code snippet, using a TreeSet. What happened to that snippet?

Piet Souris
Bartender
Posts: 4709
183
• Number of slices to send:
Optional 'thank-you' note:

Campbell Ritchie wrote:Why are you using an array of arrays rather than an ordinary array?

creates a 100x100 matrix

But talking about Streams, you can have Java do all the calculations. For instance:

statistics is of type IntSummaryStatistics, and contains the count, min, max, sum and average.

Francis Oswald
Greenhorn
Posts: 11
1
• Number of slices to send:
Optional 'thank-you' note:

Piet Souris wrote:@OP

in your opening post, you ended with a nice code snippet, using a TreeSet. What happened to that snippet?

I'm messing around with Java for only about 4 months now and couldn't make this thing work with a TreeSet constructor. Maybe at some time in the future.

Creator of Enthuware JWS+ V6
Posts: 3398
312
• Number of slices to send:
Optional 'thank-you' note:
Congratulations Francis Oswald,

Your question has made it to our Journal

Have a Cow!

 Don't get me started about those stupid light bulbs.