# Trying to write my own program : /

Show 40 post(s) from this thread on one page
Page 1 of 3 123 Last
• 09-25-2010, 01:54 AM
jdicerch
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();
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, 02:17 AM
Norm
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, 02:19 AM
jdicerch
I want whole numbers as well. Would i do something like Math.round(Math.random()*10) for 0-10?
• 09-25-2010, 02:34 AM
al_Marshy_1981
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, 02:39 AM
Norm
Quote:

I want whole numbers
You could cast the double to an int by adding (int)
• 09-25-2010, 02:58 AM
jdicerch
I was told to use Math.random. Norm you think i should do: (int)(Math.round(Math.random()*10))?
• 09-25-2010, 03:03 AM
al_Marshy_1981
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
Code:

```int num=(int) (Math.round(Math.random()*29)); System.out.println(num+1);```
• 09-25-2010, 03:12 AM
jdicerch
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, 03:17 AM
jdicerch
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, 03:20 AM
al_Marshy_1981
Quote:

Originally Posted by jdicerch
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);

yes, that is fine, you could have used the variable num throughout aswell just by reassignment e.g.

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);```
I hope you understand the code... Math.random() produces a number between 0.0 and 1.0 if you multiply this numbet by 10 as in your first piece of code you could have something like 0.8735 * 10 = 8.735 which is then rounded to a whole number by Math.round(). to get numbers that begin with 1 and the target number inclusive you must multiply by the desired high number minus 1 e.g. (1-30 multiply by 29) the add by 1 to eliminate 0 and include the high number e.g. 29+1=30. hope this helps.
• 09-25-2010, 03:26 AM
jdicerch
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:

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, 03:33 AM
al_Marshy_1981
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, 03:38 AM
jdicerch
Grr haha I'm struggling. Would I use this command? (Math.floor(Math.random()*11))
Or am I way off?
• 09-25-2010, 03:39 AM
Fubarable
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, 03:41 AM
jdicerch
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, 03:43 AM
al_Marshy_1981
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, 03:49 AM
jdicerch
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, 03:56 AM
al_Marshy_1981
Code:

```int num=(int) (Math.round(Math.random()*10));     System.out.println(num);```
there is no way this is producing any number higher than 10, so to say you are getting a number between 0-20 for it could not be true. as for the others Math.random() produces exactly that, a random number, run the program a few times and I guarantee that the code you produced for 30-70 will produce a number below 30 eventually.
• 09-25-2010, 03:59 AM
jdicerch
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, 04:02 AM
jdicerch
Would this work? Math.floor((70-29)*Math.random()) + 30;
Show 40 post(s) from this thread on one page
Page 1 of 3 123 Last