Math.random and Arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jca613
    New Member
    • Mar 2010
    • 9

    #1

    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.JOp tionPane;
    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.rando m()*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.prin t(anArray);





    }

    }
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    anArray = new int[10];
    Only holds 10 integers. You said you want 100.

    int random =(( int)(Math.rando m()*10)+99);
    This generates a random number from (99 - 109).

    Example:
    anArray[0] = ( int)(Math.rando m()*9);

    Comment

    • jca613
      New Member
      • Mar 2010
      • 9

      #3
      public class hw_7{

      public static void main(String[] args){
      int[] anArray = new int[100];
      for(int index =0; index <anArray.length ; index++){
      anArray[index] = (int) (Math.random()* 10);
      }
      for(int element = 0; element < anArray.length; element++) {
      System.out.prin tln("anArray[" + element + "]\t" + anArray[element]);
      }
      }
      }

      that is what i got so far now. count the occurrences of the digits. my output should look something liek this

      Digit:
      0 1 2 3 4 5 6 7 8 9
      Count:
      8 13 8 12 6 12 9 10 10 12


      but what can i ad to my code to make it look like that?

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        How are you going to store those count numbers? Figure out a way to store the counts.

        Then you can loop through your array, setting the storage accordingly.

        Comment

        • jca613
          New Member
          • Mar 2010
          • 9

          #5
          i honestly i have no idea.my brain is like frying trying to figure this out. do i do something like make another loop array? and put char, then count digitis.

          Comment

          Working...