Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

Join 244,291 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 956 people online right now. Registration is fast and FREE... Join Now!




random numbers from list

 
Reply to this topicStart new topic

random numbers from list, specify list of random numbers

badjava
29 Oct, 2008 - 11:35 PM
Post #1

D.I.C Regular
Group Icon

Joined: 29 Oct, 2008
Posts: 482



Thanked: 7 times
Dream Kudos: 25
My Contributions
I am going crazy trying to find specific information on the built in random method in Java and examples of the parameters that are allowed.

Specifically, rather than choosing a random number between 1 and 12:
CODE

Random randomNumbers = new Random();
mysexynumber = 1 + randomNumbers.nextInt(12);


I want to choose a random number from the list (1,4,5,8,9) and I've tried everything i can think of to modify the built in random function but can't figure out a way to do it.

Will this require my own completely custom class or does someone know a (easy oh please oh please) way to pass the parameters to one of the existing random methods?

BadJava

User is offlineProfile CardPM
+Quote Post


g00se
RE: Random Numbers From List
30 Oct, 2008 - 12:23 AM
Post #2

D.I.C Lover
Group Icon

Joined: 19 Sep, 2008
Posts: 1,042



Thanked: 104 times
My Contributions
QUOTE(badjava @ 30 Oct, 2008 - 12:35 AM) *

I want to choose a random number from the list (1,4,5,8,9) and I've tried everything i can think of to modify the built in random function but can't figure out a way to do it.


Well since you've defined the set from which you want to choose, you can just do:

CODE

final int[] CHOICES = { 1,4,5,8,9 };
int choice = CHOICES[rand.nextInt(CHOICES.length)];



QUOTE(badjava @ 30 Oct, 2008 - 12:35 AM) *

I want to choose a random number from the list (1,4,5,8,9) and I've tried everything i can think of to modify the built in random function but can't figure out a way to do it.


Well since you've defined the set from which you want to choose, you can just do:

CODE

final int[] CHOICES = { 1,4,5,8,9 };
int choice = CHOICES[rand.nextInt(CHOICES.length)];


User is offlineProfile CardPM
+Quote Post

badjava
RE: Random Numbers From List
30 Oct, 2008 - 12:30 AM
Post #3

D.I.C Regular
Group Icon

Joined: 29 Oct, 2008
Posts: 482



Thanked: 7 times
Dream Kudos: 25
My Contributions
hey goose thank you for the reply. That looks like a VERY easy way to get the results I am looking for. Do you have an idea for something around that easy without using an array?

[edit] I don't have to but I'm trying to stick as close to the built in 'randomNumbers' method as possible mostly out of pure stubbornness! laugh.gif

This post has been edited by badjava: 30 Oct, 2008 - 12:32 AM
User is offlineProfile CardPM
+Quote Post

g00se
RE: Random Numbers From List
30 Oct, 2008 - 12:35 AM
Post #4

D.I.C Lover
Group Icon

Joined: 19 Sep, 2008
Posts: 1,042



Thanked: 104 times
My Contributions
Well i'm glad to say i find it difficult to find more difficult ways of doing things ;-)

One way would be to filter out any unwanted numbers from the entire range, but that would be very inefficient
User is offlineProfile CardPM
+Quote Post

badjava
RE: Random Numbers From List
30 Oct, 2008 - 12:42 AM
Post #5

D.I.C Regular
Group Icon

Joined: 29 Oct, 2008
Posts: 482



Thanked: 7 times
Dream Kudos: 25
My Contributions
(i meant that to say trying to stick with random() btw)

Well i guess i could do an if/switch thing to only print the number if it matches the list but yeah thats ugly.

I'm not trying to make it more difficult, I just figure there has to be a way to do this with the random() method and I'm so frustrated that I can't find a complete/clear/simple break down online of this method and different ways to manipulate it to get what you want out of it... I've searched with all of the phrasing I can think of and still cant google crap for it. GRRRrrr
User is offlineProfile CardPM
+Quote Post

g00se
RE: Random Numbers From List
30 Oct, 2008 - 12:49 AM
Post #6

D.I.C Lover
Group Icon

Joined: 19 Sep, 2008
Posts: 1,042



Thanked: 104 times
My Contributions
OTOH, if you're determined in your perversity, since your range is so narrow ;-) :

CODE

        int choice = 0;
        do {
            choice = rand.nextInt(9) + 1;
        } while ("2367".indexOf("" + choice) > -1);


Your problem really is that you're trying to make Random do something more than it was designed to do, which is tantamount to trying to make it do something it was not designed to do

This post has been edited by g00se: 30 Oct, 2008 - 12:52 AM
User is offlineProfile CardPM
+Quote Post

badjava
RE: Random Numbers From List
30 Oct, 2008 - 01:06 AM
Post #7

D.I.C Regular
Group Icon

Joined: 29 Oct, 2008
Posts: 482



Thanked: 7 times
Dream Kudos: 25
My Contributions
QUOTE(g00se @ 30 Oct, 2008 - 01:49 AM) *

OTOH, if you're determined in your perversity, since your range is so narrow ;-) :

CODE

        int choice = 0;
        do {
            choice = rand.nextInt(9) + 1;
        } while ("2367".indexOf("" + choice) > -1);


Your problem really is that you're trying to make Random do something more than it was designed to do, which is tantamount to trying to make it do something it was not designed to do


AH HA!! I think that's it! Can you believe that googling 'tutorial java.util.Random' brings up hits that basically show the one same thing over and over and no simple/creative ways to get anything out of this method?? crazy.gif
User is offlineProfile CardPM
+Quote Post

badjava
RE: Random Numbers From List
30 Oct, 2008 - 01:22 AM
Post #8

D.I.C Regular
Group Icon

Joined: 29 Oct, 2008
Posts: 482



Thanked: 7 times
Dream Kudos: 25
My Contributions
QUOTE(g00se @ 30 Oct, 2008 - 01:49 AM) *

OTOH, if you're determined in your perversity, since your range is so narrow


Check out this code I just came across on this website: http://www.configure-all.com/javarandomgenerator.php

CODE

     //initialize rnd with current system time
       Random rnd = new Random(System.currentTimeMillis());

       //generate the first number

       int i = Math.abs(rnd.nextInt() % 56);

     //If number is 0 keep generate numbers until it is not 0

     while(i==0)
       i = Math.abs(rnd.nextInt() % 56);


I like the way they initialized the seed with system time, pretty slick. The % 56 thing stumps me though, yes I am that much of a noob. I thought %'number' was the modulus operator like what is the remainder after dividing by 56 but in this application it looks like they have used it to mean anynumber (thats not zero) < 56. Is this code kinda weird or do I really that much more reading to do lol

This post has been edited by badjava: 30 Oct, 2008 - 01:23 AM
User is offlineProfile CardPM
+Quote Post

g00se
RE: Random Numbers From List
30 Oct, 2008 - 01:31 AM
Post #9

D.I.C Lover
Group Icon

Joined: 19 Sep, 2008
Posts: 1,042



Thanked: 104 times
My Contributions
Your interpretation is correct. Of course it would have been a whole lot easier to just do

rnd.nextInt(56);

which is the same
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 03:51PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month