Results 1 to 3 of 3
- 09-20-2010, 04:35 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 28
- Rep Power
- 0
How to use arrays with Math.random
Introduction: hi, im lenny from sweden and i've been studying java for almost 2 weeks! i'm wondering how i can assign values to an array, 0 = heads and 1=tails and use math.random to randomize the arrays to make it a 50-50% chance.
public class Krona {
private String heads, tails;
private String throw[];
public void throw(){
throw= new String[1];
throw[0]=heads;
throw[1]=tails;
int throwOne=(int)(throw.length*Math.random());
System.out.println(throwOne);
}
public class blabla {
public static void main (String [] args){
Throw Random = new Throw();
Random.throw();
}
}
}
ERROR:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at Krona.kasta(Krona.java:14)
at mainkrona.main(mainkrona.java:7)
i dont understand this error :/
edit: turned out to be weird when i was about to translate my methods and variables from swedish to english but i hope u can understand it either way :SLast edited by LennyKosmos; 09-20-2010 at 04:40 PM.
- 09-20-2010, 04:39 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
- 09-20-2010, 04:44 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 28
- Rep Power
- 0
Similar Threads
-
Math.random not zero
By rsvr in forum New To JavaReplies: 3Last Post: 02-28-2010, 01:57 AM -
Math.random
By p0rnstar in forum New To JavaReplies: 9Last Post: 01-27-2010, 01:26 AM -
Math.random()
By Dieter in forum New To JavaReplies: 4Last Post: 09-14-2009, 09:28 AM -
Math.Random
By Java Tip in forum Java TipReplies: 0Last Post: 11-23-2007, 02:09 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks