random selection.

I've been looking at the rnadom function but cant figure it out. I have three strings lets say:

String s1 = "something1";

String s2 = "something2";

String s3 = "something3";

well i cant figure out how i could randomly select one. Random(); doesnt look like it would work. And thats the only random way. I'm use to php coding were u do Random(1, 5) and it would select a random value between the two. I could get it in php but cant excatly understand the java random procedyre. Links to anything like this or a tutorial on this or a code snipplet o anything of help would be appricated.

[618 byte] By [bronze-starDukes] at [2007-11-26 12:12:32]
# 1
Put the Strings in a List. Shuffle the list. Choose the first element.Alternatively. Put the Strings in an array. Choose a random number in the range of the array.
silverstar at 2007-7-7 14:12:33 > top of Java-index,Archived Forums,Socket Programming...
# 2
wold that be done with the random function?
bronzestar at 2007-7-7 14:12:33 > top of Java-index,Archived Forums,Socket Programming...
# 3
Put the strings in an array instead. Generate a random number with a range relative to the number of elements in the array. Then use that random number to index into the array.Edit:I knew someone would beat me to it.Message was edited by: warnerja
platinumsta at 2007-7-7 14:12:33 > top of Java-index,Archived Forums,Socket Programming...
# 4
String[] strings = new String[3]strings[0] = s1;//add the rest...String random = strings[(int)(Math.random()*2)];something like thatadd them to an
silverstar at 2007-7-7 14:12:33 > top of Java-index,Archived Forums,Socket Programming...
# 5
thanks mkoryak ill try that.
bronzestar at 2007-7-7 14:12:33 > top of Java-index,Archived Forums,Socket Programming...
# 6
*sigh*
silverstar at 2007-7-7 14:12:33 > top of Java-index,Archived Forums,Socket Programming...
# 7
> Edit:> I knew someone would beat me to it.> That's nothing my reply 1 got beat by reply 4 apparently.
silverstar at 2007-7-7 14:12:33 > top of Java-index,Archived Forums,Socket Programming...
# 8
no all were helpfull, but myoak gave me an example.
bronzestar at 2007-7-7 14:12:33 > top of Java-index,Archived Forums,Socket Programming...
# 9
while you are at trying it, try to give me the dukesto everyone else who replied to this thread i have this to say:SUCKAS!
silverstar at 2007-7-7 14:12:33 > top of Java-index,Archived Forums,Socket Programming...
# 10

> > String[] strings = new String[3]

> strings[0] = s1;

> //add the rest

> ...

> String random = strings[(int)(Math.random()*2)];

>

>

> something like that

> add them to an array

Don't use Math.random, use java.util.Random

http://java.sun.com/docs/books/effective/excursion-random.html

goldstar at 2007-7-7 14:12:33 > top of Java-index,Archived Forums,Socket Programming...
# 11

> while you are at trying it, try to give me the dukes

>

> to everyone else who replied to this thread i have

> this to say:

> SUCKAS!

Except that your example uses a different class and is more complicated than needs be and it is brittle.

This is better.

String[] strings = new String[3]

strings[0] = s1;

//add the rest

...

Random r = new Random();

String random = strings[r.nextInt(strings.length)];

silverstar at 2007-7-7 14:12:33 > top of Java-index,Archived Forums,Socket Programming...
# 12
I agree that your example is better, but I WROTE MINE FASTER SUCKA!!!ok, im done. sorry i had a long day
silverstar at 2007-7-7 14:12:33 > top of Java-index,Archived Forums,Socket Programming...