The formula for n is : n = (int) (Math.random( ) *10). If Math.random( ) gives its value as 0.1 then n= ?
1
0
0.1
1.1
Answers
Answered by
2
- The value of n is 1.
It's given that:
> n = (int)(Math.random() * 10)
• Math.random() function is used to generate random values.
When Math.random() gives it's value as 0.1:
> n = (int)(0.1 * 10)
> n = (int)(1.0)
The value of n is explicitly converted to int. Therefore:
> n = 1
Math.random(): This function returns a random number between 0 and 1.
Returned Type: double.
Modifications:
1. To generate any random integer between 1 and n:
> int x = (int)(Math.random() * n) + 1;
2. To get any random integer number between m (lower limit) and n (upper limit):
> int x = (int)((Math.random() * (n - m)) + m);
Similar questions
Computer Science,
1 month ago
Social Sciences,
2 months ago
Math,
10 months ago
Social Sciences,
10 months ago
Science,
10 months ago