# Thread: Trying to write my own program : /

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();
a = console.nextInt();
System.out.println(+a);
}
}

i know a lot is wrong, i am just doing trial and error.

Thanks!
Joey  Reply With Quote

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.  Reply With Quote

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?  Reply With Quote

4. Senior Member Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
11

## 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)  Reply With Quote

5. ## I want whole numbers
You could cast the double to an int by adding (int)  Reply With Quote

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))?  Reply With Quote

7. Senior Member Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
11

## 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);```  Reply With Quote

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  Reply With Quote

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);  Reply With Quote

10. Senior Member Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
11

##  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.

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);```
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.
Last edited by al_Marshy_1981; 09-25-2010 at 03:26 AM.  Reply With Quote

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);
}
}```  Reply With Quote

12. Senior Member Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
11

## 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.  Reply With Quote

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?  Reply With Quote

14. ## 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  Reply With Quote

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 : /  Reply With Quote

16. Senior Member Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
11

## 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....  Reply With Quote

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.  Reply With Quote

18. Senior Member Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
11

## Java 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.  Reply With Quote

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  Reply With Quote

20. Member Join Date
Sep 2010
Posts
23
Rep Power
0

## Would this work? Math.floor((70-29)*Math.random()) + 30;  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•