Results 1 to 4 of 4
Thread: Java random number
 11212009, 01:28 AM #1Member
 Join Date
 Nov 2009
 Posts
 2
 Rep Power
 0
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);
}
}
 11212009, 01:39 AM #2Member
 Join Date
 Nov 2009
 Posts
 2
 Rep Power
 0
Made a mistake here, instead of Runner ekt it is int ekt.. still wrong results though
 11212009, 09:40 AM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,131
 Blog Entries
 7
 Rep Power
 24
Care to elaborate on that 'something is wrong' part?
kind regards,
Jos
 11222009, 07:15 AM #4
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
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; 11222009 at 07:22 AM.
CodesAway  codesaway.info
writing tools that make writing code a little easier
Similar Threads

Trying to Generate Random number
By PeterFeng in forum New To JavaReplies: 10Last Post: 01142009, 09:37 AM 
Random number help
By jgonzalez14 in forum New To JavaReplies: 5Last Post: 09162008, 09:13 AM 
Random number
By jithan in forum Advanced JavaReplies: 1Last Post: 06132008, 01:42 PM 
Generate a random number
By romina in forum New To JavaReplies: 1Last Post: 08072007, 05:23 AM 
How to generate random number in java
By fernando in forum New To JavaReplies: 1Last Post: 08012007, 07:32 PM
Bookmarks