Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Math.random() and assigning values. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Math.random() and assigning values." Watch "Math.random() and assigning values." New topic
Author

Math.random() and assigning values.

Rykurex Peters
Greenhorn

Joined: Oct 28, 2012
Posts: 21
Hey all, I'm trying to design a program to print out 3 random letters (like a slot machine).
To do this I know I need to use the Math.random() method to first generate 3 numbers, multiply those by 3 and then round them down.
Then I want to assign them to their place in the alphabet by using If-Else statements.

So far all I have is this, and I know there must be a much simpler way to do it, this particular class looks like it's going to get out of hand, especially when I have to use it in the other classes! >.<

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3495
    
  13
Couple of problems that you need to fix first
1. The 'symbol' variable on line 14 is never set because the generateSymbol methd declares a local variable of the same name.
2. You don't initialise your A, B and C variables, so the assignSymbol method will always set letter to null.*

As to your problem - all characters in Java have an int value and the int values of the letter characters are consecutive. So if you want to select randomly from the characters 'A', 'B' and 'C' you just need to generate a random number between 1 and 3 (or 0 and 2) and then add the value of the 'A' character to that value. To help you, you don't need to actually know what the value of the 'A' character is as you can represent it in your code as 'A' or you can store it in a char variable.


Using this information see if you can rewrite your program to handle the letters A, B and C and then try to expand it to handle any letter.

*This doesn't actually matter because you'll find you won't need these variables


Joanne
Rykurex Peters
Greenhorn

Joined: Oct 28, 2012
Posts: 21
When you say "add the value of that character to A" what do you mean, and how do I achieve this?

I think I should say how lost I am with this task, I normally have an idea of what to do but this time I'm really clueless about it, I haven't got any idea how to approach this one
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7700
    
  20

Rykurex Peters wrote:To do this I know I need to use the Math.random() method to first generate 3 numbers, multiply those by 3 and then round them down.

Hunh? What makes you think that?

Firstly, Math.random() is NOT what you want. Have a look at the Random class (java.util.Random) and specifically its nextInt(int) method.

Secondly, why are you multiplying by 3? You said you wanted "a program to print out 3 random letters (like a slot machine)", but what you've written is a program that will print out one (and only one) of three possible values chosen at random.

My advice: StopCoding (read carefully).

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Rykurex Peters
Greenhorn

Joined: Oct 28, 2012
Posts: 21
Thanks Winston

I need something that will print out the 3 letters, and also keep track of the things like the cost of the game so far, how much the person has won, the round number etc,
Would it be best to have 3 letters printed in one class? Or 1 letter printed in this class and then CAN I reuse/refer to the class 3 times??


Also, from the link you gave me I read
public static double random()

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.

When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression

new java.util.Random()

This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else.

This method is properly synchronized to allow correct use by more than one thread. However, if many threads need to generate pseudorandom numbers at a great rate, it may reduce contention for each thread to have its own pseudorandom-number generator.


Does this mean I can use the random generator, and then I don't have to assign it to anything :S!?


Lastly, my assignment says to use, (int) (Math.random()*3 so this is what I'm trying to use but I've still no idea what to do
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7700
    
  20

Rykurex Peters wrote:I need something that will print out the 3 letters, and also keep track of the things like the cost of the game so far, how much the person has won, the round number etc,

Whoa hoss. Deal with one thing at a time - that's what good programmers do.

First write a program (or class) that can return you three random letters. Or indeed, maybe even just one (after all, you can always call it three times if you need to, or create three of them). There'll be plenty of time to add all that other stuff later.

Also, from the link you gave me I read
public static double random() ...

Not from either link I gave you, you didn't. That looks to me like the docs from the Math.random() method; and what it's basically telling you is that Math.random() actually uses the Random class under the hood.

Does this mean I can use the random generator, and then I don't have to assign it to anything :S!?

You could do, but I would advise it. Why not just store it somewhere?

Lastly, my assignment says to use, (int) (Math.random()*3 so this is what I'm trying to use but I've still no idea what to do

Well, your assignment is woefully out of date then. Random has been around for at least 4 full releases, and it's a lot easier to use. However, if you must, you must - I still don't understand where the '* 3' comes from though.

Maybe you should post the exact wording of the assignment.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Math.random() and assigning values.