• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Random sequence

 
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.






 
Marshal
Posts: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 69493
277
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: 3959
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: 69493
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 3959
155
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 2590
124
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: 3959
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 69493
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree, Piet.
 
Francis Oswald
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the input so far but......why is this not working now? I'm having a really hard time understanding loops:

 
Norm Radder
Rancher
Posts: 3795
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

 
Norm Radder
Rancher
Posts: 3795
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 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: 69493
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the code:

 
Campbell Ritchie
Marshal
Posts: 69493
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 69493
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the min/max methods take two numbers of the same type. And you have forgotten to declare count.
 
Norm Radder
Rancher
Posts: 3795
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's done. i did it with Arrays and it's working now. Mission completed.
 
Campbell Ritchie
Marshal
Posts: 69493
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 7179
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Francis Oswald
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Working code:

 
Campbell Ritchie
Marshal
Posts: 69493
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 3959
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP

in your opening post, you ended with a nice code snippet, using a TreeSet. What happened to that snippet?
 
Piet Souris
Bartender
Posts: 3959
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: 3346
303
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congratulations Francis Oswald,

Your question has made it to our Journal

Have a Cow!
 
"To do good, you actually have to do something." -- Yvon Chouinard
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic