random numbers from list

specify list of random numbers

Page 1 of 1

8 Replies - 9452 Views - Last Post: 30 October 2008 - 02:31 AM Rate Topic: -----

#1 badjava  Icon User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

random numbers from list

Post icon  Posted 30 October 2008 - 12:35 AM

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:
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

Is This A Good Question/Topic? 0
  • +

Replies To: random numbers from list

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2734
  • View blog
  • Posts: 11,515
  • Joined: 20-September 08

Re: random numbers from list

Posted 30 October 2008 - 01:23 AM

View Postbadjava, on 30 Oct, 2008 - 12:35 AM, said:

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:

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




View Postbadjava, on 30 Oct, 2008 - 12:35 AM, said:

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:

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


Was This Post Helpful? 0
  • +
  • -

#3 badjava  Icon User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: random numbers from list

Posted 30 October 2008 - 01:30 AM

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! :lol:

This post has been edited by badjava: 30 October 2008 - 01:32 AM

Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2734
  • View blog
  • Posts: 11,515
  • Joined: 20-September 08

Re: random numbers from list

Posted 30 October 2008 - 01:35 AM

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
Was This Post Helpful? 0
  • +
  • -

#5 badjava  Icon User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: random numbers from list

Posted 30 October 2008 - 01:42 AM

(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
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2734
  • View blog
  • Posts: 11,515
  • Joined: 20-September 08

Re: random numbers from list

Posted 30 October 2008 - 01:49 AM

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

		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 October 2008 - 01:52 AM

Was This Post Helpful? 1
  • +
  • -

#7 badjava  Icon User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: random numbers from list

Posted 30 October 2008 - 02:06 AM

View Postg00se, on 30 Oct, 2008 - 01:49 AM, said:

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

		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:
Was This Post Helpful? 0
  • +
  • -

#8 badjava  Icon User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: random numbers from list

Posted 30 October 2008 - 02:22 AM

View Postg00se, on 30 Oct, 2008 - 01:49 AM, said:

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...omgenerator.php

	 //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 October 2008 - 02:23 AM

Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2734
  • View blog
  • Posts: 11,515
  • Joined: 20-September 08

Re: random numbers from list

Posted 30 October 2008 - 02:31 AM

Your interpretation is correct. Of course it would have been a whole lot easier to just do

rnd.nextInt(56);

which is the same
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1