Results 1 to 6 of 6

Thread: Math.random()

  1. #1
    rajeev is offline Member
    Join Date
    Jul 2013
    Location
    india
    Posts
    15
    Rep Power
    0

    Post Math.random()

    public class StaticBlockDemo {
    static int[] values=new int[10];
    static{
    System.out.println("in side static block");
    for (int i = 0; i < 10; i++) {
    values[i]= (int) Math.random();
    }
    }
    public void listValues(){
    for (int i = 0; i < 10; i++) {
    System.out.println("values["+i+"]:"+values[i]);
    }
    }
    public static void main(String[] args) {
    StaticBlockDemo blockDemo=new StaticBlockDemo();
    StaticBlockDemo blockDemo2=new StaticBlockDemo();
    blockDemo.listValues();
    blockDemo2.listValues();
    }
    }

    the above code is printing output like this:

    in side static block
    values[0]:0
    values[1]:0
    values[2]:0
    values[3]:0
    values[4]:0
    values[5]:0
    values[6]:0
    values[7]:0
    values[8]:0
    values[9]:0
    values[0]:0
    values[1]:0
    values[2]:0
    values[3]:0
    values[4]:0
    values[5]:0
    values[6]:0
    values[7]:0
    values[8]:0
    values[9]:0


    i want to print some values except 0 , with out changing int[] values datatype, is it possible ........if possible please modify my code.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: Math.random()

    The Math.random() method generates a pseudo random number between 0 (inclusive) and 1 (exclusive); casting such a number to the int type always results in 0. Have a look at the Random class instead.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,260
    Rep Power
    6

    Default Re: Math.random()

    How to find the documentation;

    Google: java 7 Random
    Result: Random (Java Platform SE 7 )
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    rajeev is offline Member
    Join Date
    Jul 2013
    Location
    india
    Posts
    15
    Rep Power
    0

    Default Re: Math.random()

    thnx JosAH...........

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,912
    Rep Power
    5

    Default Re: Math.random()

    You can also multiply Math.random() by some integer N. Then you may cast the resulting value to an int to get a value between 0 and N-1. But using the Random class is more efficient for this type of application.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Math.random()

    whenever I want to reply a thread ... I always see josAH already answered

Similar Threads

  1. Math.random Question
    By noble in forum New To Java
    Replies: 21
    Last Post: 09-16-2010, 03:17 AM
  2. Math.random not zero
    By rsvr in forum New To Java
    Replies: 3
    Last Post: 02-28-2010, 02:57 AM
  3. Math.random
    By p0rnstar in forum New To Java
    Replies: 9
    Last Post: 01-27-2010, 02:26 AM
  4. Math.random()
    By Dieter in forum New To Java
    Replies: 4
    Last Post: 09-14-2009, 10:28 AM
  5. Math.Random
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-23-2007, 03:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •