Question could someone explain the below code to me?

tangara

Member
Joined
Feb 5, 2016
Messages
6
Programming Experience
1-3
Hi,

I am a beginner in C# and am desperately looking for help.

Basically, I do not understand why the following line in this method below:
public static void Main(string [] args)
{
    Console.Write("Enter a number: ");
    int n = Convert.ToInt32(Console.ReadLine());

    Random random = new Random();
    double g = random.NextDouble() * (n - 1) + 1;

    while (Maths.Abs(g*g - n) > 0.00001)
    {
        g = (g + n/g)/2;
    }

    Console.WriteLine(g);
}

why NextDouble() has to multiply by (n - 1) + 1?

Hope someone can explain to me.

Thanks.
 
Last edited by a moderator:
why NextDouble() has to multiply by (n - 1) + 1?

The number entered by the user appears to define a range. The Random.NextDouble method returns a random value X in the range 0.0 <= X < 1.0. By running that value through that operation you would end up with a value Y in the range 1.0 <= Y < n. So, if the user enters the number 53 then g will be less than 53 and no less than 1 before entering the `while` loop.
 
The number entered by the user appears to define a range. The Random.NextDouble method returns a random value X in the range 0.0 <= X < 1.0. By running that value through that operation you would end up with a value Y in the range 1.0 <= Y < n. So, if the user enters the number 53 then g will be less than 53 and no less than 1 before entering the `while` loop.

Hello John,

Thank you for your kind reply.

However, it is so difficult for beginner to figure out that the code has to be multiplied by (n - 1) + 1 for it to realize the 0.0 <= X < 1.0 and return a 1.0 <= Y < n...

Or rather for someone that is not mathematically inclined....

Is there a simpler way to break down the code to achieve the same effect ?
 
I don't know where you got that code from but good practice dictates that the author of the code provide comments for stuff like. Unfortunately, that often doesn't happen. Good comments don't tell you WHAT the code is doing because you can see that. Good comments tell you WHY the code is doing what it's doing. In this case, the multiplication is to scale the result and addition is to shift it. For instance, if n is 10 then the multiplication scales the result up 9 times, while the addition shifts it 1 in the positive direction. There's not really a simpler way to do it than two basic mathematical operations.
 
I don't know where you got that code from but good practice dictates that the author of the code provide comments for stuff like. Unfortunately, that often doesn't happen. Good comments don't tell you WHAT the code is doing because you can see that. Good comments tell you WHY the code is doing what it's doing. In this case, the multiplication is to scale the result and addition is to shift it. For instance, if n is 10 then the multiplication scales the result up 9 times, while the addition shifts it 1 in the positive direction. There's not really a simpler way to do it than two basic mathematical operations.

Hi John,

Thank yo so much for explaining things to me. I seem to understand than before. However, if this kind of question if it is given to me, I am sure I will not be able to come out with a code like that in order to get the g as the square root that has a deviation of 5 decimiel places.

...my heartfelt thanks to you.
 
Back
Top Bottom