Re: Random number
- From: dagon@xxxxxxxxx (Mark Rafn)
- Date: Tue, 6 Feb 2007 18:53:40 -0800
Christo <chris.me.white@xxxxxxxxxxx> wrote:
can anyone show me the code to do this very basic code to simply output the
random number that has been generated to the console?
public class RandomCoin {
/** flip a coin. */
public static void main(String[] args) {
java.util.Random rand = new java.util.Random();
System.out.println(rand.nextBoolean() ? "heads" : "tails");
}
}
Of course, if you prefer some other random type than boolean, call the
appropriate method - see the javadoc.
If you're using it for cryptography, you might prefer SecureRandom.
--
Mark Rafn dagon@xxxxxxxxx <http://www.dagon.net/>
.
- References:
- Random number
- From: Christo
- Random number
- Prev by Date: Random number
- Next by Date: Re: Random number
- Previous by thread: Random number
- Next by thread: Re: Random number
- Index(es):