Results 1 to 6 of 6
  1. #1
    jgonzalez14 is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Default Random number help

    Hi all,

    I need help with a random number feature. I want a random number between 1 and 26 (including 1 and 26). I have this

    letter =(int) (Math.random() * 26 + 1);

    Since Math.random() is 0 - .9999999, how do i get whole numbers?

    should I do this:

    so right now i am not getting accurate whole numbers. can anyone help?


    Thanks

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    10

    Default

    Random has a nextInt() that takes an upper bound. There are short and well known ways to get the number in a range.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    jgonzalez14 is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Default

    I was looking into that but I have to keep the Random() function. Thanks tho. Is it possible to get some insight with the Random feature?

  4. #4
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    10

    Smile basically simple

    Well, in general what we do is int num = Math.random() times range plus floor. It is simple, you just have to do a timeout and think about it.

    Some number times ( 0 - 1 ) gives, bascially, zero to the number. Then we often have a range so it is upper limit minus lower limit times random plus floor.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  5. #5
    racerxadam is offline Member
    Join Date
    Jul 2008
    Posts
    68
    Rep Power
    0

    Default

    I created a quick test class and only got numbers between 1 and 26 inclusive using basically what you posted.
    Java Code:
    public class TestRandom {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    
    		for (int i = 0; i < 50000; i++) {
    			int letter = (int)(Math.random() * 26 + 1);
    			System.out.println(letter);
    			if(letter == 0 || letter > 26){
    				throw new RuntimeException("The value is out of range" + letter);
    			}
    		}
    	}
    }

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    21

    Default

    You can do this easily in this way as well.

    Java Code:
        public static void main(String[] args) {
            Random randomGenerator = new Random();
            
            for(int i = 0; i < 15; i++) {
                int j = randomGenerator.nextInt(26);
                System.out.println(j + 1);
            }
        }

Similar Threads

  1. trying to add up random numbers into one number
    By pjr5043 in forum New To Java
    Replies: 4
    Last Post: 09-15-2008, 02:20 PM
  2. Random number
    By jithan in forum Advanced Java
    Replies: 1
    Last Post: 06-13-2008, 01:42 PM
  3. Generate a random number
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:23 AM
  4. How to generate random number in java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 07:32 PM
  5. Random Image on Refresh (FROM Folder not set number)
    By QuinnMal in forum Java Servlet
    Replies: 1
    Last Post: 07-06-2007, 07:27 PM

Posting Permissions

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