For Programmers: Free Programming Magazines  


Home > Archive > Java Help > November 2005 > random number output









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author random number output
patrick_woflian

2005-11-24, 9:56 pm

hello, i just wondering how you write out the code in java to acheive
an output of a random number between one and twelve.. any takers?
thanks very much

Oliver Wong

2005-11-24, 9:56 pm

"patrick_woflian" <gingercrock@hotmail.com> wrote in message
news:1132850424.939534.192340@o13g2000cwo.googlegroups.com...
> hello, i just wondering how you write out the code in java to acheive
> an output of a random number between one and twelve.. any takers?
> thanks very much
>


see java.util.Random

http://java.sun.com/j2se/1.5.0/docs...til/Random.html

- Oliver


Allan Bruce

2005-11-24, 9:56 pm


"patrick_woflian" <gingercrock@hotmail.com> wrote in message
news:1132850424.939534.192340@o13g2000cwo.googlegroups.com...
> hello, i just wondering how you write out the code in java to acheive
> an output of a random number between one and twelve.. any takers?
> thanks very much
>


Random r = new Random(); // in java.util.Random
int randomNumber = r.nextInt(12)+1;

Allan


Thomas Fritsch

2005-11-24, 9:56 pm

patrick_woflian wrote:
> hello, i just wondering how you write out the code in java to acheive
> an output of a random number between one and twelve.. any takers?
> thanks very much
>

Reading the JavaDocs of java.util.Random will help.

--
"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

Ravi

2005-11-26, 3:58 am

Hi ,

U can also genarate random numbers in this way

int x=(int)Math.random()%12+1

Bye,
Ravi.

Roedy Green

2005-11-26, 9:57 pm

On 25 Nov 2005 22:18:34 -0800, "Ravi" <ravindra4me@gmail.com> wrote,
quoted or indirectly quoted someone who said :

>int x=(int)Math.random()%12+1


that won't work. For why see
http://mindprod.com/jgloss/pseudorandom.html#BADCODE
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com