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?

[358 byte] By [antonio_montanaa] at [2007-11-26 15:04:56]
# 1

> 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

prometheuzza at 2007-7-8 8:54:41 > top of Java-index,Java Essentials,New To Java...
# 2

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

antonio_montanaa at 2007-7-8 8:54:41 > top of Java-index,Java Essentials,New To Java...
# 3
teachers these day..they always piss me off..if i was rly antonio montana..i'd shoot em..but is it possible to use math.random to pick out a position?
antonio_montanaa at 2007-7-8 8:54:41 > top of Java-index,Java Essentials,New To Java...
# 4

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.

jverda at 2007-7-8 8:54:41 > top of Java-index,Java Essentials,New To Java...
# 5
oh okay..I kinda get..do u mind providing an example?
antonio_montanaa at 2007-7-8 8:54:41 > top of Java-index,Java Essentials,New To Java...
# 6
> oh okay..I kinda get..do u mind providing an example?How about you try it first?
prometheuzza at 2007-7-8 8:54:41 > top of Java-index,Java Essentials,New To Java...
# 7

> 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.

jverda at 2007-7-8 8:54:41 > top of Java-index,Java Essentials,New To Java...