Results 1 to 20 of 51
- 09-25-2010, 02:54 AM #1
Member
- 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
- 09-25-2010, 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.
- 09-25-2010, 03:19 AM #3
Member
- 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 0-10?
- 09-25-2010, 03:34 AM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
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)
- 09-25-2010, 03:39 AM #5I want whole numbers
- 09-25-2010, 03:58 AM #6
Member
- 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))?
- 09-25-2010, 04:03 AM #7
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
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);
- 09-25-2010, 04:12 AM #8
Member
- 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
- 09-25-2010, 04:17 AM #9
Member
- 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);
- 09-25-2010, 04:20 AM #10
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
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; 09-25-2010 at 04:26 AM.
- 09-25-2010, 04:26 AM #11
Member
- 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); } }
- 09-25-2010, 04:33 AM #12
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
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.
- 09-25-2010, 04:38 AM #13
Member
- 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
- 09-25-2010, 04:41 AM #15
Member
- Join Date
- Sep 2010
- Posts
- 23
- Rep Power
- 0
So for 0-10 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 : /
- 09-25-2010, 04:43 AM #16
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
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....
- 09-25-2010, 04:49 AM #17
Member
- 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 0-20, 1-30, 30-70. Or is it not including 0 or 70 or something? Sorry for being a pain in the butt.
- 09-25-2010, 04:56 AM #18
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
Java Code:int num=(int) (Math.round(Math.random()*10)); System.out.println(num);
- 09-25-2010, 04:59 AM #19
Member
- 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 0-10.
*29 is giving me a number from 0-29. and *69 is giving me a number from 0-69? So you're saying I need to set the base(where my random number will start). Right? or no
- 09-25-2010, 05:02 AM #20
Member
- 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: 09-04-2010, 07:12 PM -
How to write two server program in java
By priyamurugar in forum NetworkingReplies: 1Last Post: 04-27-2010, 12:20 PM -
is it possible to write program with out thread
By makpandian in forum Threads and SynchronizationReplies: 3Last Post: 12-21-2008, 06:41 PM -
How to write interceptor program in struts2?
By vasu in forum Web FrameworksReplies: 1Last Post: 10-07-2008, 08:53 AM -
need help to write Program in JAVA
By maneibr in forum New To JavaReplies: 1Last Post: 03-13-2008, 02:28 PM
Bookmarks