Hi- I need a function that will return a random integer that can be only -1, 0, or 1. Thanks?
Genius!!!!!!11!
Tom Hawtin - tackline
2009-05-23 15:13:12
Thought about submitting an Enterprise solution, but it's proprietary.
Apocalisp
2009-05-23 15:14:20
Ya, thanks..............
2009-05-23 15:15:41
Why not generate a random number from 17 to 19 and subtract 18?
Bombe
2009-05-23 15:53:14
@Bombe, you've opened the door to a very dark place. Random number from 0 to 1000 modulus 3 - 1? ;-)
Bob Cross
2009-05-23 17:34:14
I think we should use a randomized recursive Fibonacci function there...
PhiLho
2009-05-23 18:25:57
+11
A:
As Apocalisp wrote, you could do something like:
import java.util.Random;
Random generator = new Random();
int randomIndex = generator.nextInt( 3 ) - 1;
Justin Ethier
2009-05-23 15:13:17
Yes, it does. I was hoping to edge out Justin with the javdoc, though. Next time, I should just go for "First P0st!!!111!!@@One!!"
Bob Cross
2009-05-23 15:22:15
First post. Then edit it with pointless code and links to the JavaDoc (just in case anyone doesn't have their favourite JavaDoc location bookmarked). Then replace it with a paraphrase of the second person's correct answer.
Tom Hawtin - tackline
2009-05-23 15:26:11
Awesome! At least everyone else can be confident the code works since its the same one-liner :)
Justin Ethier
2009-05-23 15:26:19
Oh I didn't notice you linked to the javadoc... :(. I voted for this guy cause he had the import statement, which I usually forget to do, causing me to waste 10 minutes trying to find the problem.
2009-05-23 15:31:06
Yeah, my two line solution was waaaaaay better because of the javadoc link.... ;-) To be honest, I never ever think about import statements anymore. Whatever IDE you're using (Netbeans or Eclipse at our office) will just import if / when necessary.
Bob Cross
2009-05-23 17:33:07
+3
A:
Random random = new Random();
int value = random.nextInt(3) - 1;
Bob Cross
2009-05-23 15:13:52