AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Random number

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> comp.lang.java
View previous topic :: View next topic  
Author Message
Christo
Guest





PostPosted: Wed Feb 07, 2007 6:03 am    Post subject: Random number Reply with quote



hello I am new to java very new and am hoping someone can offer me some
help.

I know that i need to use java.util.Random somewhere to generate a random
number

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?

I would really appreciate this, I can work from there to expand on what i
need ti to do

TIA

Christo
Back to top
Mark Rafn
Guest





PostPosted: Wed Feb 07, 2007 8:10 am    Post subject: Re: Random number Reply with quote



Christo <chris.me.white (AT) hotmail (DOT) com> wrote:
Quote:
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 (AT) dagon (DOT) net <http://www.dagon.net/>
Back to top
j1mb0jay
Guest





PostPosted: Thu Feb 08, 2007 4:18 am    Post subject: Re: Random number Reply with quote



This will create you a random double.

//Creates random double.
double random = Math.random();

//Turns double into a number >=1
double Random = random * 10;

//Cast the double as an int (removes decimals)
int xOry = (int)Random;

--
Regards JJ (UWA)

"Christo" <chris.me.white (AT) hotmail (DOT) com> wrote in message
news:L6ydnbRgzPDCilTYnZ2dnUVZ8vednZ2d (AT) bt (DOT) com...
Quote:
hello I am new to java very new and am hoping someone can offer me some
help.

I know that i need to use java.util.Random somewhere to generate a random
number

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?

I would really appreciate this, I can work from there to expand on what i
need ti to do

TIA

Christo
Back to top
Ian Shef
Guest





PostPosted: Fri Feb 09, 2007 1:15 am    Post subject: Re: Random number Reply with quote

"j1mb0jay" <jap6 (AT) aber (DOT) ac.uk> wrote in news:1170886729.974948 (AT) leri (DOT) aber.ac.uk:

Quote:
This will create you a random double.

//Creates random double.
double random = Math.random();

//Turns double into a number >=1

No, because random may be a number less than 0.1
For example, 0.04382 * 10 becomes 0.4382

Perhaps you meant that this expands the range of the random number?

Quote:
double Random = random * 10;

//Cast the double as an int (removes decimals)

....by truncation, may not be what you want. Consider Math.round().

Quote:
int xOry = (int)Random;


There is some good information on generation of pseudorandom numbers in Java
at
http://mindprod.com/jgloss/pseudorandom.html





--
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *
Back to top
j1mb0jay
Guest





PostPosted: Fri Feb 09, 2007 5:46 am    Post subject: Re: Random number Reply with quote

Sorry have only started messing with java when I started this course at
university, otherwise use to C#, was just trying to help, but if didn't know
it could return 0.0xxx I must edit code to allow for this mistake. Thanks
for the comment.

--
Regards JJ (UWA)

"Ian Shef" <invalid (AT) avoiding (DOT) spam> wrote in message
news:Xns98D17CAFD9762vaj4088ianshef (AT) 138 (DOT) 126.254.210...
Quote:
"j1mb0jay" <jap6 (AT) aber (DOT) ac.uk> wrote in
news:1170886729.974948 (AT) leri (DOT) aber.ac.uk:

This will create you a random double.

//Creates random double.
double random = Math.random();

//Turns double into a number >=1

No, because random may be a number less than 0.1
For example, 0.04382 * 10 becomes 0.4382

Perhaps you meant that this expands the range of the random number?

double Random = random * 10;

//Cast the double as an int (removes decimals)

...by truncation, may not be what you want. Consider Math.round().

int xOry = (int)Random;


There is some good information on generation of pseudorandom numbers in
Java
at
http://mindprod.com/jgloss/pseudorandom.html





--
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *
Back to top
Rune Zedeler
Guest





PostPosted: Sat Feb 10, 2007 5:58 am    Post subject: Re: Random number Reply with quote

Ian Shef wrote:

Quote:
...by truncation, may not be what you want.

Usually truncation is what you want. Round is normally never what you want.
For instance to throw a dice, use 1+(int)(Math.random()*6).

Math.round(Math.random()*10) gives 0 with 5% probability, 10 with 5%
probability and 1-9 with 10% probability each.

-Rune
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> comp.lang.java All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.