Results 1 to 20 of 51
 09252010, 02:54 AM #1Member
 Join Date
 Sep 2010
 Posts
 23
 Rep Power
 0
Trying to write my own program : /
Okay well I am practicing for a test and our teacher gave us some practice problems. I have to create random numbers using the static random() method. Part1 asks me to create 3 integers. one between 0 and 20, another between 1 and 30, and a third between 30 and 70. I also need to store these values for later use.
so far i have this (dont make fun, im new to this):
import java.lang.Math;
import java.util.Scanner;
public class ranNum
{
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
int a=((int)(Math.random()*10));
double b=Math.random();
double c=Math.random();
//System.out.println("Please input:");
a = console.nextInt();
System.out.println(+a);
}
}
i know a lot is wrong, i am just doing trial and error.
Thanks!
Joey
 09252010, 03:17 AM #2
First you need to work out the math for taking the output of the random method and mapping it to a number in the ranges you want.
Do you have the logic for how to do that?
There are two parts to the logic. One is to multiply the value returned by random to get one of the possible numbers in the range. For example if the range is 10 to 19 inclusive there are 10 possible values: 0 to 9.
The next part of the logic is to adjust the range of numbers so it maps to the actual range you want. For a range of 10 to 19 you would need to add 10 to the value from the multipling part (which gave you a number from 0 to 9) to map it to 10 to 19.
 09252010, 03:19 AM #3Member
 Join Date
 Sep 2010
 Posts
 23
 Rep Power
 0
I want whole numbers as well. Would i do something like Math.round(Math.random()*10) for 010?
 09252010, 03:34 AM #4Senior Member
 Join Date
 Feb 2010
 Location
 Waterford, Ireland
 Posts
 748
 Rep Power
 5
Not sure if Math.random() is a total requirement but this is a little less complex to figure out:
Random (Java 2 Platform SE v1.4.2)
 09252010, 03:39 AM #5I want whole numbers
 09252010, 03:58 AM #6Member
 Join Date
 Sep 2010
 Posts
 23
 Rep Power
 0
I was told to use Math.random. Norm you think i should do: (int)(Math.round(Math.random()*10))?
 09252010, 04:03 AM #7Senior Member
 Join Date
 Feb 2010
 Location
 Waterford, Ireland
 Posts
 748
 Rep Power
 5
if you want numbers between 0 and 10 then yes use that, multiply by 20 for numbers between 0 and 20.
for between 1 and 30 multiply by 29 and add 1.
e.g
Java Code:int num=(int) (Math.round(Math.random()*29)); System.out.println(num+1);
 09252010, 04:12 AM #8Member
 Join Date
 Sep 2010
 Posts
 23
 Rep Power
 0
Works perfect but when i go to do my second number using: int num1=(int) (Math.round(Math.random()*29+1));
I get this error:The local variable num1 is never read
 09252010, 04:17 AM #9Member
 Join Date
 Sep 2010
 Posts
 23
 Rep Power
 0
Actually it's working now. Is this correct? : int num=(int) (Math.round(Math.random()*10));
System.out.println(num);
int num1=(int) (Math.round(Math.random()*29));
System.out.println(num1+1);
int num2=(int) (Math.round(Math.random()*69));
System.out.println(num2+1);
 09252010, 04:20 AM #10Senior Member
 Join Date
 Feb 2010
 Location
 Waterford, Ireland
 Posts
 748
 Rep Power
 5
yes, that is fine, you could have used the variable num throughout aswell just by reassignment e.g.
Java Code:int num=(int) (Math.round(Math.random()*10)); System.out.println(num); num=(int) (Math.round(Math.random()*29)); System.out.println(num+1); num=(int) (Math.round(Math.random()*69)); System.out.println(num+1);
Last edited by al_Marshy_1981; 09252010 at 04:26 AM.
 09252010, 04:26 AM #11Member
 Join Date
 Sep 2010
 Posts
 23
 Rep Power
 0
I have to keep it stored for later use : /
Here were the complete directions, can you tell me if this fulfills them: Your job is first to generate three integers. The first should be between 0 and 20(inclusive), the second between 1 and 30(inclusive), and the third 30 and 70(inclusive). In the course of doing this, you should store your generated integers in variables for future use.
Here is my complete code:
Java Code:import java.lang.Math; import java.util.Scanner; public class ranNum { public static void main(String[] args) { Scanner console = new Scanner(System.in); int num=(int) (Math.round(Math.random()*10)); System.out.println(num); int num1=(int) (Math.round(Math.random()*29)); System.out.println(num1+1); int num2=(int) (Math.round(Math.random()*69)); System.out.println(num2+1); } }
 09252010, 04:33 AM #12Senior Member
 Join Date
 Feb 2010
 Location
 Waterford, Ireland
 Posts
 748
 Rep Power
 5
not quite
between 0 and 20 is very similar to 0 and 10...
you have 1 and 30 correct
you got confused with 30 and 70..... your base number is 30 not 0, remember Math.random() gives a number between 0.0 and 1.0, you need to increase that 0.0 compensate for 30 somehow
think about it some more then get back to me with your best attempt.
 09252010, 04:38 AM #13Member
 Join Date
 Sep 2010
 Posts
 23
 Rep Power
 0
Grr haha I'm struggling. Would I use this command? (Math.floor(Math.random()*11))
Or am I way off?

No. Just use Math.random() but you'll need to multiply it by something and then add something to it. This is nothing more than very basic algebra. Figure it out on paper first. For instance in your last example, 0 will map to 30 and 1 will map to 70. So if 0 is X, Y will be 30 and then if X is 1, Y is 70. Knowing this, figure out M and K:
Y = M*X + K
 09252010, 04:41 AM #15Member
 Join Date
 Sep 2010
 Posts
 23
 Rep Power
 0
So for 010 Math.random()*29+30? Or the other way around? I understand what you're saying but I can't comprehend where to put it in the equation : /
 09252010, 04:43 AM #16Senior Member
 Join Date
 Feb 2010
 Location
 Waterford, Ireland
 Posts
 748
 Rep Power
 5
quick tip for getting 0 and 20. 1st read my post #10, then seriously get out your calculator and multiply any number between 0.0 and 1.0 by 20 and see the result, then round the result, it should give you a good insight into what Math.floor() is doing to Math.random(). You will notice the further you come to 1.0 the closer you will get to 20 and the closer you come to 0.0 the nearer your answer is 0.
try multiplying 0.95 by 20....
 09252010, 04:49 AM #17Member
 Join Date
 Sep 2010
 Posts
 23
 Rep Power
 0
Could you maybe explain why the code is wrong? I compiled it and it does generate whole numbers between 020, 130, 3070. Or is it not including 0 or 70 or something? Sorry for being a pain in the butt.
 09252010, 04:56 AM #18Senior Member
 Join Date
 Feb 2010
 Location
 Waterford, Ireland
 Posts
 748
 Rep Power
 5
Java Code:int num=(int) (Math.round(Math.random()*10)); System.out.println(num);
 09252010, 04:59 AM #19Member
 Join Date
 Sep 2010
 Posts
 23
 Rep Power
 0
Tell me if i am right or wrong. my Math.random()*10 is giving me a number from 010.
*29 is giving me a number from 029. and *69 is giving me a number from 069? So you're saying I need to set the base(where my random number will start). Right? or no
 09252010, 05:02 AM #20Member
 Join Date
 Sep 2010
 Posts
 23
 Rep Power
 0
Similar Threads

which classes to write..how to program this?
By sony1 in forum New To JavaReplies: 7Last Post: 09042010, 07:12 PM 
How to write two server program in java
By priyamurugar in forum NetworkingReplies: 1Last Post: 04272010, 12:20 PM 
is it possible to write program with out thread
By makpandian in forum Threads and SynchronizationReplies: 3Last Post: 12212008, 06:41 PM 
How to write interceptor program in struts2?
By vasu in forum Web FrameworksReplies: 1Last Post: 10072008, 08:53 AM 
need help to write Program in JAVA
By maneibr in forum New To JavaReplies: 1Last Post: 03132008, 02:28 PM
Bookmarks