• 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Math.random array need help with my code  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Okey this is what i got. It ourputs random numbers between 0 and 100 , 25 times and that's what i want so that part is done.
But after it have printed out this random numbers 25 times i want the program to select 5 of the outprinted numbers at random and then again print out these 5 selected random numbers from the list of 25.
Any help is greatly appreciated.
 
lowercase baba
Posts: 12745
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So take it one step at a time.

You have a bunch of numbers. How would you pick one at random?
 
Marshal
Posts: 4623
299
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Brian-kurt Kibbler wrote:It ourputs random numbers between 0 and 100 , 25 times

You sure about that? (Spoiler alert: it doesn't)

Brian-kurt Kibbler wrote:But after it have printed out this random numbers 25 times i want the program to select 5 of the outprinted numbers at random and then again print out these 5 selected random numbers from the list of 25.

Answer me this: Is there a difference between selecting 5 random numbers from a list of 25 random numbers, and just selecting 5 random numbers?
 
Brian-kurt Kibbler
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im not sure actually as it is the first time that im working with a array.
Should i make another for loop simular to the first one but with a output ?
 
Brian-kurt Kibbler
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ye i noticed it only outputs numbers between 0- 10 , and if i exchange the <100 to <1000 it only outputs numbers over 10 to 100.
How is this possible ?
 
Tim Cooke
Marshal
Posts: 4623
299
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what your code is doing:

What you have created is a thing that counts the occurrence of each integer between 0 and 24 that is obtained from calling java.util.Random 100 times
 
Sheriff
Posts: 13454
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Brian-kurt Kibbler wrote:
It ourputs random numbers between 0 and 100 , 25 times


Tim Cooke wrote:You sure about that? (Spoiler alert: it doesn't)


@Brian-kurt Kibbler: When in doubt, refer to the API JavaDocs: http://docs.oracle.com/javase/8/docs/api/java/util/Random.html#nextInt-int-

Which one did you want as your upper bound, 100 or 25? Which one did you use in your program?
 
Tim Cooke
Marshal
Posts: 4623
299
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu, take another look. It's worse than that. The Random.nextInt() call is not being used to choose what value to put in the array, it's being used to select the index of the array.

You want to do something like this:
 
Junilu Lacar
Sheriff
Posts: 13454
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Cooke wrote:Junilu, take another look. It's worse than that. The Random.nextInt() call is not being used to choose what value to put in the array, it's being used to select the index of the array.


It's not too bad, actually. OP just didn't code what he said he wanted to do. The code right now reminds me of the game we call Sungka back home, you might otherwise know it as Mancala, except this version simulates dropping shells in random holes instead of going in sequence around the board.

OP just needs to understand how to do these tasks separately:

1. Generate a random number N
2. Do something N times
3. Pick out the Nth element of an array

If OP understands the mechanics of each one of these, then it's just a matter of combining them in just the right way to do what he wants to do.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!