Results 1 to 8 of 8
Thread: Math.Random and Arrays
 04102010, 06:26 PM #1Member
 Join Date
 Apr 2010
 Posts
 4
 Rep Power
 0
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);
}
}
 04102010, 06:38 PM #2
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 23
Welcome to forum. :)
First of all tell me how to generate number from 1 to 9?
 04102010, 06:48 PM #3Member
 Join Date
 Apr 2010
 Posts
 4
 Rep Power
 0
thank you. umm is it Random randomGenerator = new Random();
? or it is is Math.random right. i am still new to java
 04102010, 07:01 PM #4
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 23
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); }
 04102010, 07:21 PM #5Member
 Join Date
 Apr 2010
 Posts
 4
 Rep Power
 0
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
 04102010, 07:26 PM #6
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 23
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]); } }
 04102010, 07:45 PM #7Member
 Join Date
 Apr 2010
 Posts
 4
 Rep Power
 0
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 09 arrays is stored. because i have to use a array of 10 integers that says count where the arrays stores (09) i i have to do a counting array
 04122010, 05:08 AM #8
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 23
Did you run that code, and see the result?
Java Code:iArray[index] = (int) (Math.random()*8 + 1);
Similar Threads

Math.random not zero
By rsvr in forum New To JavaReplies: 3Last Post: 02282010, 02:57 AM 
Math.random
By p0rnstar in forum New To JavaReplies: 9Last Post: 01272010, 02:26 AM 
Math.random()
By Dieter in forum New To JavaReplies: 4Last Post: 09142009, 10:28 AM 
Math.Random
By Java Tip in forum Java TipReplies: 0Last Post: 11232007, 03:09 PM 
math.random function help
By katie in forum New To JavaReplies: 2Last Post: 08062007, 04:31 AM
Bookmarks