• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I create a simple random number generator for the number between 1 -10?  RSS feed

 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a little nothing project, but I wanted to create a random number generator for a silly game where the user guesses the number. How can I do this? I have used google, but they are using LOG statements, which I have no idea what it does. Thanks in advance.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a couple of ways in java.


First in the class java.lang.Math it has a method random() that gives you a number between 0 and 1. You can multiply that to get the range you are after.

Alternatively you can use the class java.util.Random
The Random class has a handy: nextInt(lessThanThisNumber) method for you


 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just use the java.util.Random class from the standard library.
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the simple code, I think I am mixing up Double, Int and Randoms. I am having issue it seems because I can't switch between an int and a double, but I am not sure how to use doubles in this case - or if I even need to. I want to generate a number from one to ten only.


 
Victor Bogo
Greenhorn
Posts: 25
AngularJS Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David De wrote:Here is the simple code, I think I am mixing up Double, Int and Randoms. I am having issue it seems because I can't switch between an int and a double, but I am not sure how to use doubles in this case - or if I even need to. I want to generate a number from one to ten only.




When you call rand.nextInt(10) on line 16 JAVA generate one randomic value but it ignores the result since you just called the method and do not asigned the value to any variable. Change this line to int someIntName = rand.nextInt(10)

On line 31 you suposed to compare two values, however using = you tell java to asign the right variable value to left one. Change this operator to == if you intend to compare!

Comparing int and double isn't a problem, JAVA cast the lower one to the biggest type. In this case, befote the comparision is validated java cast the int to double!

EDIT

Forgot to mention: Change the compared value on line 30 to the new variable created on rand.nextInt(10)



 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are wanting an integer from the user, then you should read an integer, rather than a double number (which implies a decimal point)

ie change rrand to be of type int rather than double, and use textInput.nextInt()
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I changed the code but still getting errors. I changed the double to int, created a variable for the random number, compared == and now I am getting "newRand cannot be resolved to this variable"
Did I miss something?

 
Victor Bogo
Greenhorn
Posts: 25
AngularJS Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You forgot the type bafore newRand. Declare it as int!
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


On line 16 of the code above you are referring to a variable called "newRand"
Have you declared this variable? What type should it be?
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it, thank you.

The last thing in this little program is how can I make each IF statement jump to the random number generator? This only works if they user types in anything but Mike or David. I would like it to go to the random number generator regardless. Is this what is called a method?
 
Victor Bogo
Greenhorn
Posts: 25
AngularJS Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David De wrote:Got it, thank you.

The last thing in this little program is how can I make each IF statement jump to the random number generator? This only works if they user types in anything but Mike or David. I would like it to go to the random number generator regardless. Is this what is called a method?


The main method declared in your code is a special method where everything starts. Just like it, you can create your own methods. I sugest you to read Oracle's tutorial Defining Methods on this link

Basicaly, it is a block of code that you can call anytime depending on the acess modifiers and some other rules. This way you can call the rand anytime you need it!

See this exemple below(considere as it is inside a class and with proper imports):
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at your if/then/else structure.

The number guessing game is nested within the final "else" block.


So right now, it will only play the numbers game if you reach that last else block.

So if you want the game to ALWAYS be played, you need to move it out of the if/then/else blocks
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So could I make the random number generator a separate class instead?
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can put the random generator in a utility class, in a static method. That way you can call
MyUtilities.randomInt(1, 10)
and get the required “random” number. You can read a nice discussion about different ways to get “random” numbers here; if you search you will find lots of people ask that sort of question.

I shall give you a start with the utility class: note the reason for the private constructor is explained in the Java® Language Specification (=JLS).Read the link to the old post carefully; there is often a lot of confusion about what possible results you can get from the Random object's methods. Also have a look at the JLS; that is one of the few parts of the JLS that are easy to read
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I am trying to work with methods, and what I did seems to work, please let me know if I am doing this correctly. I'm getting an error, but it runs. There are no errors in eclipse, just on the "run" does it warn me. grrrrrr

 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You aren't indenting your code correctly. You should have one more level to the right every time you have an open brace { And closing braces } at the ends of methods should be on their own lines. Line 25 is particularly confusing.
It is difficult to see in your post where each method finishes and the next starts. You should also have one blank line between successive methods. This is what the indentation should have looked like:-It is not easy because you have used tabs, but I think I have got it nearly right. We have some formatting suggestions of our own.

By the way, why are you saying between 1 and 10?
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does no good simply telling us you get an error; we need to know what error you get.
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the formatting information. Right now I am trying to get it to work and forget about how to format. I am relieved when it works period!
I had it between 1 and 10 just so I could test it easily.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might have said 1 to 10 but why? What does that method call actually return? Did you read the old discussion which I provided a link to?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!