Results 1 to 17 of 17
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Need help converting random number to a int

  1. #1
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Need help converting random number to a int

    Just what the title says heres my code
    Java Code:
    import java.io.*;
    import java.util.*;
    public class prog210c {
    	public static void main(String args[])
    	{
    		Random rndm = new Random();
    		 double rm = (Random) rndm;
    		 int count = 0;
    		 int x = 0;
    		for(int j = 0; j < 1; j++)
    		{
    			rndm.nextInt(100);
    			if(x != rndm)
    			{
    				System.out.println("Sorry try again...");
    				count++;
    			}
    		}
    	}
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Need help converting random number to a int

    Does the code compile? If not, and you can't understand the compiler's messages copy and post them. Someone is sure to be able to explain what they mean.

    ---

    There are quite a few variables in that code. Make sure you have a clear idea what you expect each one to be (and how they act together to do whatever you want the program to do - you don't say what that is). In fact it is a good idea to choose as variables something that is descriptive. j is a bit of an exception because it is clear what it is doing. But count for example: what is it supposed to be a count of? Decide, then consider changing it to whateverCount.

    Also follow Java coding conventions and change the name of the class to Prog210c. That's a small thing, but it's a habit worth getting in to.

  3. #3
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Re: Need help converting random number to a int

    Quote Originally Posted by pbrockway2 View Post
    Does the code compile? If not, and you can't understand the compiler's messages copy and post them. Someone is sure to be able to explain what they mean.

    ---

    There are quite a few variables in that code. Make sure you have a clear idea what you expect each one to be (and how they act together to do whatever you want the program to do - you don't say what that is). In fact it is a good idea to choose as variables something that is descriptive. j is a bit of an exception because it is clear what it is doing. But count for example: what is it supposed to be a count of? Decide, then consider changing it to whateverCount.

    Also follow Java coding conventions and change the name of the class to Prog210c. That's a small thing, but it's a habit worth getting in to.
    No the code dosent compile lol thats the problem. The reason its not compiling is cause i cant make the if statment compairing a int to a random.
    Java Code:
     Random rndm = new Random();
    
             double rm = (Random) rndm;
    here you can see that i am generating a random number and then i am trying to make a double variable the same as the random number generated but i cant figure out how to do it thats why i posted here.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default Re: Need help converting random number to a int

    Quote Originally Posted by MrPosotive View Post
    Java Code:
     Random rndm = new Random();
    
             double rm = (Random) rndm;
    here you can see that i am generating a random number and then i am trying to make a double variable the same as the random number generated but i cant figure out how to do it thats why i posted here.
    No, you're not generating a random number at all; you're trying to convert a random number generator to a double. A generator isn't a number itself ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Re: Need help converting random number to a int

    Quote Originally Posted by JosAH View Post
    No, you're not generating a random number at all; you're trying to convert a random number generator to a double. A generator isn't a number itself ...

    kind regards,

    Jos
    Well thanks for letting me know what that code is doing. Now can you please help me get the number stored as a int or double.

  6. #6
    JBelg is offline Member
    Join Date
    Oct 2012
    Posts
    36
    Rep Power
    0

    Default Re: Need help converting random number to a int

    Random (Java Platform SE 7 )

    Have a look at that.

  7. #7
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Re: Need help converting random number to a int

    Quote Originally Posted by JBelg View Post
    Random (Java Platform SE 7 )

    Have a look at that.
    I looked at it but its not much help. I needto find a way to store the random number as a int.

  8. #8
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Re: Need help converting random number to a int

    Acutally i figured it out. but now ive run into one last problem, whenever it goes into the if loop i need to be able to keep entering numbers so that the person keeps guessing but i can enter it once and then it wont let me enter it again for some reason even though it repeats the loop
    Java Code:
    import java.io.*;
    import java.util.*;
    public class prog210c {
    	public static void main(String args[])
    	{
    	int a = (int) (Math.random()*20);
    		 
    		
    		 int count = 0;
    		 int x = 0;
    		for(int j = 0; j < 1; j++)
    		{
    			System.out.println("Please guess a number ");
    			 int c = 0;
    			if(c != a )
    			{
    				Scanner b = new Scanner(System.in);
    				  c = b.nextInt();
    				System.out.println("Sorry try again...");
    				count++;
    				
    			}
    			else
    			{
    				System.out.println ("Your correct! The number was " + a + " You guessed " + count + "times");
    			}
    		}
    	}
    }

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default Re: Need help converting random number to a int

    Quote Originally Posted by MrPosotive View Post
    Well thanks for letting me know what that code is doing. Now can you please help me get the number stored as a int or double.
    Your code isn't doing anything because it doesn't even get passed the compiler. Reading the API for the Random class and reading up on the basics of the Java language might do wonders ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Re: Need help converting random number to a int

    Quote Originally Posted by JosAH View Post
    Your code isn't doing anything because it doesn't even get passed the compiler. Reading the API for the Random class and reading up on the basics of the Java language might do wonders ...

    kind regards,

    Jos
    It was compiling without the one part that i was trying to convert the random generator to a int. Your really not much help for a mod are you?

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Need help converting random number to a int

    Your really not much help for a mod are you?
    I think we can all do without the personal comments.

    Jos, like everyone else here, moderator or not, offers their help generously. And that generosity ought to be appreciated.

    For better help, apply yourself to tidying up your code, your grammar, and your descriptions of your problems. The last includes precisely specifying observed and intended program behaviour and actually posting compiler messages.

    even though it repeats the loop
    The loop you posted in #8, whatever it is intended to do, will execute exactly once as you can see from the number of times it prints "Please guess a number ".
    Kagiso likes this.

  12. #12
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Re: Need help converting random number to a int

    Quote Originally Posted by pbrockway2 View Post
    I think we can all do without the personal comments.

    The loop you posted in #8, whatever it is intended to do, will execute exactly once as you can see from the number of times it prints "Please guess a number ".
    Yea that loop was pointless it was apart of another thing i tried and it didnt worki forgot it was there. But the problem is still there The loop here
    Java Code:
    if(c != a )
    			{
    				Scanner b = new Scanner(System.in);
    				  c = b.nextInt();
    				System.out.println("Sorry try again...");
    				count++;
    				b.nextInt();
    its still not looping correctly, i need to be able to input a number until the imputed number equals the random number that was generated but every time i run it this is the output " Please guess a number
    1
    Sorry try again...
    2"
    and then the program terminates.

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Need help converting random number to a int

    That isn't a loop at all. Possibly a do-while loop could be used, or a while loop and break when you're done. (I have a thing against do-while, others seem cagey about breaking...)

    Java Code:
    Declare and set numberToBeGuessed
    Print "Guess a number"
    WHILE true (ie forever)
        Declare and get guess
        IF guess is numberToBeGuessed
            break
        ELSE
            Print "Guess again"
    END WHILE
    The important thing is the algorithm - the steps. Decide on this before you code. A good algorithm should read like ordinary language: it's worth a few key presses to say numberToBeGuessed for a number to be guessed as opposed to 'a'.

    Decide on whether the algorithm sketched out above would do what you want. Code it (even if it doesn't do everything you want) and deal with compiler messages. Note you aren't fighting the compiler - compiler messages do not mean "the code does not wrok", rather they are intended as helpful messages to assist you better express what you mean in Java. Test the sucessfully compiled code to ensure it really does do (if partially) what you intend.

    Decide how you would extend it to count the guesses (what's a good name for a variable that counts the number of guesses? where should the counter be declared? what should its initial value be? where should it be incremented?). Again code, compile, test.

    If you want to limit the number of guesses, decide where you would include that check. Code, compile, test. Perhaps at this point consider whether other constructs would more fluently express the desire to limit the number of guesses. For example a for loop: decide on an algorithm involving a for loop, code that, compile and test it.
    Last edited by pbrockway2; 11-12-2012 at 12:01 AM.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default Re: Need help converting random number to a int

    Quote Originally Posted by MrPosotive View Post
    It was compiling without the one part that i was trying to convert the random generator to a int.]
    That doesn't surprise me one bit; you can ruin a perfectly fine program (it compiles and runs ok) by adding one single line that doesn't make sense at all; your '(double)Random' was such a line.


    Your really not much help for a mod are you?
    I help people who want to be helped.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Re: Need help converting random number to a int

    Quote Originally Posted by JosAH View Post




    I help people who want to be helped.
    I wanted help, you pretty much told me what i already knew, and being sarcastic telling me that Reading Java basics will do wonders was the most usless thing any one has told me on these forums so far. I needed help trying to figure out a problem and my last "Solution" didnt work therefor it wasnt compiling i thought i was just doing one little thing wrong.

  16. #16
    JBelg is offline Member
    Join Date
    Oct 2012
    Posts
    36
    Rep Power
    0

    Default Re: Need help converting random number to a int

    Random is itself is not a number, it's an Object that is able to generate completely random numbers.

    To generate a random number from such an Random object you need to ask it. So in your case you made a
    Java Code:
    Random rndm = new Random();
    this is good, next you need to call one if it's method which will return a number (int, double, long ...)
    Java Code:
    rndm.nextInt();
    .

    If this doesn't help you, you'll have to go back and study the basics of Java.

  17. #17
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Re: Need help converting random number to a int

    Quote Originally Posted by JBelg View Post
    Random is itself is not a number, it's an Object that is able to generate completely random numbers.

    To generate a random number from such an Random object you need to ask it. So in your case you made a
    Java Code:
    Random rndm = new Random();
    this is good, next you need to call one if it's method which will return a number (int, double, long ...)
    Java Code:
    rndm.nextInt();
    .

    If this doesn't help you, you'll have to go back and study the basics of Java.
    Lol yea i figured that out with the help of some one above, thanks though!

Similar Threads

  1. Random number
    By _Chip in forum New To Java
    Replies: 4
    Last Post: 11-03-2012, 07:06 PM
  2. Converting Number to Base 8
    By nighteagle2 in forum New To Java
    Replies: 9
    Last Post: 02-17-2012, 02:38 AM
  3. Converting whole number into decimal
    By jim01 in forum New To Java
    Replies: 2
    Last Post: 09-23-2010, 07:58 PM
  4. A Number Converting Program!
    By WastedxYears in forum New To Java
    Replies: 2
    Last Post: 01-09-2010, 12:47 AM
  5. Random number help
    By jgonzalez14 in forum New To Java
    Replies: 5
    Last Post: 09-16-2008, 09:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •