Results 1 to 8 of 8
  1. #1
    jca613 is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default Math.Random and Arrays

    this what i got so far. i understand arrays and Math.random i think,but i need to use Math.random and arrays to make 100 random integers between 1 -9 what am i doing wrong? and what am i missing


    import java.util.*;

    import javax.swing.JOptionPane;
    public class hw_7 {

    public static void main(String[] args){
    Scanner input=new Scanner(System.in);
    int Random =input.nextInt();
    int[] anArray;

    anArray = new int[10];
    int random =(( int)(Math.random()*10)+99);
    anArray[0] = 0;
    anArray[1] = 1;
    anArray[2] = 2;
    anArray[3] = 3;
    anArray[4] = 4;
    anArray[5] =5;
    anArray[6] = 6;
    anArray[7] = 7;
    anArray[8] = 8;
    anArray[9] = 9;
    System.out.print(anArray);





    }

    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    jca613 is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    thank you. umm is it Random randomGenerator = new Random();
    ? or it is is Math.random right. i am still new to java

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yea, but what you've done is not correct, you cannot generate number between 1 and 9.

    Java Code:
        public static void main(String[] args) {
            Random objRandom = new Random();
    
            int iRandom = objRandom.nextInt(9) + 1;
    
            System.out.println(iRandom);
        }

  5. #5
    jca613 is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    so if i do something like your code you just posted it will work abit with the arrays,but does that mean i still can use Math.random

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Of course not, I just want you to give a hint, why added extra values and all.

    Java Code:
        public static void main(String[] args) {
            Random objRandom = new Random();
            int[] iArray = new int[100];
    
            for(int index = 0; index < iArray.length; index++) {
                iArray[index] = (int) (Math.random()*8 + 1);
            }
    
            for(int element = 0; element < iArray.length; element++) {
                System.out.println(iArray[element]);
            }
        }

  7. #7
    jca613 is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    ahhhh i think i get it. i need to put a for statment in to my cod to make the array to work. you are giving me alot of ideas.but that doesnt explain where 0-9 arrays is stored. because i have to use a array of 10 integers that says count where the arrays stores (0-9) i i have to do a counting array

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Did you run that code, and see the result?

    Java Code:
    iArray[index] = (int) (Math.random()*8 + 1);
    In the above live what happen?

Similar Threads

  1. Math.random not zero
    By rsvr in forum New To Java
    Replies: 3
    Last Post: 02-28-2010, 01:57 AM
  2. Math.random
    By p0rnstar in forum New To Java
    Replies: 9
    Last Post: 01-27-2010, 01:26 AM
  3. Math.random()
    By Dieter in forum New To Java
    Replies: 4
    Last Post: 09-14-2009, 09:28 AM
  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
  •