• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

randon number

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I try to print out 6 random numbers from 40 numbers
ex( 20,5,1,40,31,29)
the program which I have writen is ok, but I don't want that the program writes a random number more than one time.
ex 20,5,1,40,31,29) is ok
ex 20,5,5,40,31,29) is wrong. // how do I fix it?
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once a number has been selected, set it to -1 (or some other invalid value). Before displaying a number, make sure it's valid. Otherwise, select another.
 
john mor
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I want to print out 6 nbrs

for(int i=0; i<6; i++)
and set a nbr to -1
the program dose not print out 6 different nbr, may be 5 or 4 nbrs.
 
Mark Van Tuyl
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct. If you have selected an invalid number, you must decrement the counter.
 
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
What does your code look like? How do you store those 40 numbers in your program; in an array or one of the Java collections?

If you have the numbers in a java.util.List, you can use the method java.util.Collections.shuffle(...) to shuffle the elements in the list, and then you just take the first 6 numbers out of the shuffled list.
 
Sasparilla and fresh horses for all my men! You will see to it, won't you tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic