Results 1 to 3 of 3
Thread: Is Random() Only For Numbers?
- 02-07-2011, 08:58 AM #1
Is Random() Only For Numbers?
I have just recently begun to play around with the "Random()" utility in Java, though I have known about it for awhile now.
All to tutorials I have followed the taught about random() dealt with numbers.
What I am trying to build is a random quote generator which involves arrays and strings.
Basically what I'm asking is: is there a way this can work? or is Random() only for numbers?
- 02-07-2011, 09:09 AM #2
- Join Date
- Sep 2008
- Voorschoten, the Netherlands
- Blog Entries
- Rep Power
Yes, a Random object can only generate pseudo random numbers; but your problem doesn't really exist though: if you have n quotes simply generate a pseudo random number in the range [0, n) (see the nextInt(...) method) and select that particular quote.
JosThe only person who got everything done by Friday was Robinson Crusoe.
- 02-07-2011, 11:02 AM #3
you can store your strings in collection that implements a list or an array and then generate a random number and use this number to get a random string from your collection with get(int index) or array[index]. so there is no need that Random() generate something else than numbers.
- By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM
- By checkmylongboarding in forum New To JavaReplies: 2Last Post: 01-12-2009, 06:47 AM
- By jithan in forum Advanced JavaReplies: 3Last Post: 06-14-2008, 02:04 PM
- By carlos123 in forum New To JavaReplies: 4Last Post: 01-17-2008, 11:44 PM
- By carlos123 in forum New To JavaReplies: 1Last Post: 12-22-2007, 03:56 AM