Answered # math.random()

Featured Reply coil Discussion Starter abby2589 -2 Discussion Starter abby2589 -2 coil Featured Reply extemer -17 Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...

0

```
private static void randomNumber() {
int rand;
int min = -10;
int max = 10;
System.out.println(" ");
for (int i = 0; i < 10; i++) {
rand = (int) (Math.random() * (max - min + 1) ) + min;
System.out.print(" " + rand);
}
System.out.println("\n");
}
```

can anyone explain to me line 10: `rand = (int) (Math.random() * (max - min + 1) ) + min;`

Thank you!

*Edited 3 Years Ago by happygeek*: fixed formatting

1

First, the Math.random() method on its own generates a random number that is greater or equal to 0, but less than 1.

Obviously, decimal numbers are rather difficult to use, so we cast the result to an int.

(int)(Math.random()) will always return 0, because the method never returns anything greater than or equal to one, and casting to int doesn't round.

If the possible only result is 0, it isn't very useful. Thus, we multiply results then cast.

(int)(Math.random()*5) will produce 0, 1, 2, 3, or 4. If it returns anything less than 0.2, for example, 0.15, that multiplied by 5 is 0.75, which when cast becomes 0. If it returns, say 0.5, then after multiplying it becomes 2.5, and then after casting becomes 2.

Note: You will never get the number of which it is multiplied by, in this case 5. That's because even if it returns 0.9999, that multiplied by 5 is still less than 5, and thus after casting becomes 4.

If you don't want numbers starting at 0, then simply add the number on, but also subtract it from the high bound. Let's say you want numbers between 2 and 5.

(int)(Math.random()*5+2) can produce numbers up to 6, because if Math.random() returns 0.9999, multiplying it by 5 is around 4, and then adding 2 to it makes it 6.

Thus, the proper way to do it is (int)(Math.random()*(5-2)+2)

*Edited 6 Years Ago by coil*: n/a

0

Now I understand. Thank you!! :)

can I have another question??

how can I store the value of int rand in an array?

I used this code

```
int [] store = new int [9];
store[a] = rand;
for (int i = 0; i < 10; i++) {
System.out.print(" " + store[a]);
```

It only prints the last digit of int rand.

*Edited 6 Years Ago by abby2589*: n/a

0

Now I understand. Thank you!! :)

can I have another question??

how can I store the value of int rand in an array?

I used this code

```
int [] store = new int [9];
store[a] = rand;
for (int i = 0; i < 10; i++) {
System.out.print(" " + store[a]);
```

It only prints the last digit of int rand.

*Edited 6 Years Ago by abby2589*: n/a

0

Hm. It should work...are you getting any errors? Also, put a System.out.print call before you set the value of store[a] to rand, that displays the value of rand. If the value printed there and the value printed out by store[a] are the same, there's something wrong with the way you generated the number "rand".

You might want to get rid of your for-loop (unrelated), unless you actually do want to print out 10 times the value of store[a].

1

check this

class check{

public static void main(String[] args) {

int[] a=new int [10];

int rand;

int min = -10;

int max = 10;

System.out.println(" ");

for (int i = 0; i < 10; i++) {

rand = (int) (Math.random() * (max - min + 1) ) + min;

a*=rand;
System.out.print("\n"+a );*

}

System.out.println("\n");

}}

This question has already been answered. Start a new discussion instead.

Recommended Articles

Hi. so this is actually a continuation from another question of mineHere but i was advised to start a new thread as the original question was already answered.

This is the result of previous question answered :

code for the listbox - datagridview interaction

At the top of the code ...

the function that I created to find the ...