Results 1 to 4 of 4
  1. #1
    Mezgrath is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default Java random number

    Hey everyone, I am a beginner at Java ( totally ). And i wanted to create my first java program with the little things I know that will generate a random number in a defined domain I choose. ( eg a random number between [x,y] )

    But something is wrong and I dont know why and where to look for a solution, so I post here.
    Thanks very much, please explain me what is my fault.

    import java.io.*;
    class Runner
    {
    int a;
    int b;
    int x;
    Runner (int x, int y)
    {
    a=x;
    b=y;
    }
    Runner ekt()
    {
    x=a+(int)(Math.random()*b);
    return x;
    }
    }

    class RanNum
    {
    public static void main(String[] args) throws IOException
    {
    int x;
    int a;
    int b;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Write the first number of the domain");
    a=(int)br.read();
    System.out.println("Write the last number of the domain");
    BufferedReader nt=new BufferedReader(new InputStreamReader(System.in));
    b=(int)nt.read();
    Runner RanCon=new Runner(a,b);
    x=RanCon.ekt();
    System.out.println("The random number is: "+x);
    }
    }

  2. #2
    Mezgrath is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default

    Made a mistake here, instead of Runner ekt it is int ekt.. still wrong results though

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

    Default

    Care to elaborate on that 'something is wrong' part?

    kind regards,

    Jos

  4. #4
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    When you say "random number", do you mean a random integer, or a random floating point value? From your code, it seems a random integer, so I'll go with that.


    In your code you ask for two values, the "start" and "end" value. Math.random() returns a number, x, such that 0.0 < x < 1.0

    So, to return a number between start and end (exclusive), do the following

    Java Code:
    1) Take your number
    	0.0 <= x < 1.0
    2) Multiply it by (end - start)
    	0.0 <= x * (end - start) < (end - start)
    3) Add "start"
    	start <= x * (end - start) + start < end
    Thus, you number is Math.random * (end - start) + start, which will be between start and end (exclusive).


    To have the value between start and end (inclusive), multiply by (end - start + 1) and take the floor of the value.

    So at step 3 you get
    start <= x * (end - start + 1) + start < end + 1

    After you take the floor
    start <= Math.floor(x * (end - start + 1) + start) <= end


    Note: this method will not work (due to overflow) if (end - start + 1) causes an overflow.

    For example, if "start" is Integer.MIN_VALUE and "end" is Integer.MAX_VALUE, (end - start + 1) is ZERO!

    end = 2^31 - 1
    start = -2^31
    -start = -2^31 (incorrectly)

    end - start + 1 = (2^31 - 1) - 2^31 + 1 = 0 (big problem)
    Last edited by CodesAway; 11-22-2009 at 06:22 AM.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

Similar Threads

  1. Trying to Generate Random number
    By PeterFeng in forum New To Java
    Replies: 10
    Last Post: 01-14-2009, 08:37 AM
  2. Random number help
    By jgonzalez14 in forum New To Java
    Replies: 5
    Last Post: 09-16-2008, 09:13 AM
  3. Random number
    By jithan in forum Advanced Java
    Replies: 1
    Last Post: 06-13-2008, 01:42 PM
  4. Generate a random number
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:23 AM
  5. How to generate random number in java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 07:32 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
  •