array question..can i randomly select a value
I'm trying to make a mini slots game where the user will "push the handle" and the computer will pick a number from 3 separate arrays.Of course, related arrays will be in play to see if the numbers match. My question: Is it possible to pick a position randomly from an array? So if there's an array of ints, is there a way to pick a random position?
> I'm trying to make a mini slots game where the user
> will "push the handle" and the computer will pick a
> number from 3 separate arrays.Of course, related
> arrays will be in play to see if the numbers match.
> My question: Is it possible to pick a position
> randomly from an array? So if there's an array of
> ints, is there a way to pick a random position?
Yes. Look at the java.util.Random class:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html
sry i forgot to mention one thing..im only allowed to use what i kno..that is basic loops, for while, if statements, but i cant use random.util..i kno it sounds retarded i forgot to mention this..is there way i can incorporate math.random into an array to pick out a position..sorry for the trouble
You're allowed to use math.random but not java.util.Random? That's dumb, but yes, you can do it.
All you have to do is transform a number in the range 0 <= x < 1 to the range 0 <= x < N, where N is the size of your array. That's just simple arithmetic, so I'll leave the details up to you.
> oh okay..I kinda get..do u mind providing an example?
Forget about the array for now. Just write a small test program that calls math.random and prints out the value, so that you know how to use that.
Then think about how you might take the result of math.random and get a different number out of it. Write a tiny bit more code and test it so that you can see that it does what you think it does.
Then figure out how to apply the results of the above to getting a random element from your array.
If you get stuck, post your work and your specific question.