Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2008

Tip: Looking for answers? Try searching our database.

Random number

Thread view: 
Christo - 07 Feb 2007 00:03 GMT
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
Mark Rafn - 07 Feb 2007 02:53 GMT
>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@dagon.net    <http://www.dagon.net/>
j1mb0jay - 07 Feb 2007 22:18 GMT
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;

Signature

Regards JJ (UWA)

> hello I am new to java very new and am hoping someone can offer me some
> help.
[quoted text clipped - 11 lines]
>
> Christo
Ian Shef - 08 Feb 2007 19:15 GMT
> 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

Signature

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

j1mb0jay - 08 Feb 2007 23:46 GMT
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.

Signature

Regards JJ (UWA)

>> This will create you a random double.
>>
[quoted text clipped - 20 lines]
> at
> http://mindprod.com/jgloss/pseudorandom.html
Rune Zedeler - 09 Feb 2007 23:58 GMT
> ...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
Frank Stallone - 06 Apr 2008 21:12 GMT
> hello I am new to java very new and am hoping someone can offer me some
> help.
[quoted text clipped - 11 lines]
>
> Christo
Here would be a way to do a random number between  1 and 50

here is math in the java API to help you understand how floor and random,
etc work:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Math.html

public class MyRandomNumbers {
   
    public static void main(String[] args) {
        int myNumber;
        myNumber = (int) Math.floor(Math.random() * 50 + 1);
        System.out.println(myNumber);
    }   

}

Then again with it being over a year later, I hope you're past this
point. ;)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.