• 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

How a generate a Random number and trim it to one or two digits.

 
Ranch Hand
Posts: 598
3
jQuery Google App Engine Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The base class has a method called getRandomNumber() which will return some random integer
value. I need to use this method to generate a random integer value in such a way that I get values between 1 to 15. Also , I need to fill the array ‘array’ with random numbers from 1 to 15 without repeating any of the numbers. Currently , I have no idea how to do that. All I know a For loop is required here , also an IF block so I can make sure I am entering numbers from 1 to 15. Hints please.
 
Ranch Hand
Posts: 679
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

will give you a random number between 1 and 15.
You just need to keep repeating this until you have all the numbers between 1 and 15.
However, as the numbers are random that could theoretically never happen.

A better way is to add the numbers 1 to 15 to a List, use Collections.shuffle to shuffle the list and then generate an array from that list.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stuart A. Burkett wrote:
will give you a random number between 1 and 15.


@Bobby: And possibly an even better way is:unless, of course, you're obliged to use the methods provided.

Winston

[Edit] Corrected after Rob's spot-on post.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stuart A. Burkett wrote:
will give you a random number between 1 and 15.


getRandomNumber() % 14 will give you a number between 0 and 13 (inclusive), so (getRandomNumber() % 14) + 1 will give you a number between 1 and 14, not 15.
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the requirement is to have an array with all values between 1 and 15 but in any random order, why not just fill the array in sequence and then shuffle the array?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:getRandomNumber() % 14 will give you a number between 0 and 13 (inclusive), so (getRandomNumber() % 14) + 1 will give you a number between 1 and 14, not 15.


I refer you to Fred's signature
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob is correct, as long as the left operand for the remainder operator is not negative.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stuart A. Burkett wrote:

Rob Spoor wrote:getRandomNumber() % 14 will give you a number between 0 and 13 (inclusive), so (getRandomNumber() % 14) + 1 will give you a number between 1 and 14, not 15.


I refer you to Fred's signature


I have been known to change my .sig on occasion. but currently, it says this:

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

 
Bobby Sharma
Ranch Hand
Posts: 598
3
jQuery Google App Engine Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you all. You helped me a lot.
 
reply
    Bookmark Topic Watch Topic
  • New Topic