Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: display the generated Math.random number

  1. #1
    Junior Member PsYNus's Avatar
    Join Date
    Sep 2011
    Location
    Johannesburg, South Africa
    Posts
    10
    My Mood
    Inspired
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default display the generated Math.random number

    I've been playing around with some coding and asci values. My program below will return an asci value generated by the Math.random method. Is there another method that i may use to display that value that the Math.random generates?
    Please let me know if i didnt open this thread properly

    public class CharList
    {
    public static void main(String[] args)
    {
    char myFirstChar;
    myFirstChar = (char) (Math.random()); //A
    //System.out.println(myFirstChar);

    for (int i = 1; i<0; i++);
    {
    myFirstChar = 1;
    //System.out.println(myFirstChar);
    }

    System.out.println(myFirstChar);

    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: display the generated Math.random number

    I'm not sure what you mean by "another method that i may use to display that value". Do you mean in a GUI? Something else?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member PsYNus's Avatar
    Join Date
    Sep 2011
    Location
    Johannesburg, South Africa
    Posts
    10
    My Mood
    Inspired
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: display the generated Math.random number

    Oh sorry i was wondering if there is some code that i can add to the program that will return that randomly generated number from the Math.random

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: display the generated Math.random number

    I'm still really not positive what you mean, but you could either create a method that calls it for you, or you could look into the Random class.

    But now that I'm looking at your code a little closer, it doesn't make a ton of sense. Math.random() returns a number between 0 (inclusive) and 1 (exclusive), so converting it to a char will always give you zero, won't it? And what is that for loop supposed to be doing?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member PsYNus's Avatar
    Join Date
    Sep 2011
    Location
    Johannesburg, South Africa
    Posts
    10
    My Mood
    Inspired
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: display the generated Math.random number

    Still kind of new with java so i thought i'd throw that loop in there to see what it would do. should i not do that? so that math.random line will only return a value between 0 and 0.999 recuring?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: display the generated Math.random number

    Quote Originally Posted by PsYNus View Post
    Still kind of new with java so i thought i'd throw that loop in there to see what it would do. should i not do that?
    That depends on what you want it to do. Recommended reading: The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Quote Originally Posted by PsYNus View Post
    so that math.random line will only return a value between 0 and 0.999 recuring?
    You tell me. What does the API say? It is your new best friend: Java Platform SE 6
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member PsYNus's Avatar
    Join Date
    Sep 2011
    Location
    Johannesburg, South Africa
    Posts
    10
    My Mood
    Inspired
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: display the generated Math.random number

    is there a way i could download the API? i dont have an internet connection at home :<

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: display the generated Math.random number

    Quote Originally Posted by PsYNus View Post
    is there a way i could download the API? i dont have an internet connection at home :<
    Yeah. It's available towards the bottom of this page: Java SE Downloads
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member PsYNus's Avatar
    Join Date
    Sep 2011
    Location
    Johannesburg, South Africa
    Posts
    10
    My Mood
    Inspired
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: display the generated Math.random number

    oh i think i see now how the math.random can work. it will generate a random number between 0 (including) and 0.999 recuring (including) and you have to times that by 100 to get a whole number. is that correct?

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: display the generated Math.random number

    Quote Originally Posted by PsYNus View Post
    oh i think i see now how the math.random can work. it will generate a random number between 0 (including) and 0.999 recuring (including) and you have to times that by 100 to get a whole number. is that correct?
    Almost. You multiply it by whatever you want your maximum (non-inclusive) to be. If you want a char, check out the upper bounds for them.

    Also, don't be afraid to try these things before you ask. It's going to be faster and more reliable for you, plus it's good to get in the habit of writing little test programs. What happened when you tried it?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Junior Member PsYNus's Avatar
    Join Date
    Sep 2011
    Location
    Johannesburg, South Africa
    Posts
    10
    My Mood
    Inspired
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: display the generated Math.random number

    i've altered the code (it now gives the numerical value for the asci symbol) for example: 90.7456468755941 is the numerical value for: . So i'm gonna find a method that will round the number down

  12. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: display the generated Math.random number

    Converting it to a char should round it down for you...
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  2. Math.Random()
    By xionyus in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 26th, 2011, 10:22 PM
  3. Creating a new class and dont know how to generate a random number math code
    By beatlebaby70 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 15th, 2011, 03:03 PM
  4. How to returned random number to original number?
    By i4ba1 in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 19th, 2011, 04:35 AM
  5. Generation of random number using random class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 16th, 2009, 06:10 AM