Computer Science, asked by zackwallangdey, 2 months ago

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 anindyaadhikari13
2

\texttt{\textsf{\large{\underline{Answer}:}}}

  • The value of n is 1.

\texttt{\textsf{\large{\underline{Do you know why ?}:}}}

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

\texttt{\textsf{\large{\underline{Learn More}:}}}

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