Results 1 to 5 of 5

Thread: Math.random()

  1. #1
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default Math.random()

    Hi guys,

    i'm having problems generating random numbers in an array.

    for some reason i can't see the problem.

    i want to generate random number in a simple array but the output only returns 1.

    Can some1 open my eyes and tell me what i'm doing wrong?

    Here is the code:

    Java Code:
     public static void main(String[] args) {
    
            int [] generate = new int[7];
    
    
              for (int i = 0; i < generate.length; i++) {
                    generate[i] = ((int)Math.random() * 6) + 1;
    
                    System.out.println(generate[i]);
    
            }
    sorry for the dumbish question.

    Thanks in advance
    Last edited by Dieter; 09-14-2009 at 09:07 AM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Math.random() returns a number greater than 0 and less than 1.
    Casting that number will to int will always give 0.
    You should be able to work out why the rest wen the way it did from there.

  3. #3
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    Seems i allready found the problem.
    Sorry for posting this.

    The problem i had was that Math.random() goes from 0.0 to 1.0 and i said with int that he had to trow everyting after the flouting point away.

    works just fine now...

    Java Code:
    public static void main(String[] args) {
    
            int [] generate = new int[7];
    
    
              for (int i = 0; i < generate.length; i++) {
                    generate[i] = [B](int)(Math.random() * 6) + 1;[/B]
    
                    System.out.println(generate[i]);
    
            }
    Sorry again for posting this noobish question :)

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Also, use java.util.Random and it's nextInt method(s), rather than Math.random, and you can avoid having to do this sort of stuff for yourself, as well as getting better (although still not especially good) random number generation.

  5. #5
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    Thx for the tip :)

    Gonna try to apply that to my application now :)

Similar Threads

  1. Array of instances using Math.random()
    By xgi1008 in forum New To Java
    Replies: 16
    Last Post: 01-25-2011, 11:10 PM
  2. [SOLVED] Help with math.random
    By tomiu in forum New To Java
    Replies: 10
    Last Post: 12-21-2008, 09:55 PM
  3. Formatting isbn number with Math.random()
    By dns77x7 in forum New To Java
    Replies: 11
    Last Post: 09-21-2008, 06:02 PM
  4. Math.Random
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-23-2007, 02:09 PM
  5. math.random function help
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 03:31 AM

Posting Permissions

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