• Status: Solved
• Priority: Medium
• Security: Public
• Views: 935

random.nextdouble

Hello,

i have trouble using the rand.NextDouble()

for example i want to use
``````rand.NextDouble(12345678912345, 123456789123456);
``````

but i get this erro
``````Error	1	No overload for method 'NextDouble' takes 2 arguments
``````

but when i use the random generator for an integer like this
``````rand.Next(2345, 123456);
``````
it works

thank you
0
kensy11
2 Solutions

Commented:
I think NextDouble function takes real numbers as parameters instead of integers, so try this:

rand.NextDouble(12345678912345.0, 123456789123456.0);

and let me know if it works
0

Commented:
Sorry, I was wrong. NextDouble rand method on c# has no parameters, so you should do something like this:

http://stackoverflow.com/questions/6162640/c-random-nextdouble-and-including-borders-of-the-custom-interval
0

Author Commented:
is there an way to create random numbers of length 15 (long)

using rand.Next(,); or something similar to it
0

Commented:

``````Random rand = new Random();
const long upperBound = 1000000000000000;

long result = (long)(upperBound * rand.NextDouble());
``````

That would return a result between 0 and 999999999999999, inclusive. If you want to start at 100000000000000, then we would have to modify the logic a bit.
0

Commented:
Actually, I think I'm not 100% correct on the above.
0

Commented:
Double numbers has more precision than long numbers, so it wouldn't be a problem. If you want a long number, the example provided by kaufmed should work. If you want double, the link I provided should be helpful.

The idea is always the same: NextDouble() returns a random number between 0 and 1, but with a huge precision (double precision, this is a 64 bits float number). All you have to do is just to manipulate this number to get what you want: multiply it by the range of numbers you want, and adds the initial number you want.

For example if you want a number between 3 and 10 (not included), do the following:

result = 3 + rand.NextDouble()*7;

where 7 is 10-3

If you want to include 10 then add 1 more (8 instead of 7).

Hope it helps.
0

Middle School Assistant TeacherCommented:
``````        private void button1_Click(object sender, EventArgs e)
{
long value = RandomNumber(15);
label1.Text = value.ToString();
}

private Random R = new Random();

private long RandomNumber(int length)
{
if (length >= 1 && length <= 18)
{
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= length; i++)
{
if (i == 1)
{
sb.Append(R.Next(1, 10).ToString()); // 1 to 9 inclusive
}
else
{
sb.Append(R.Next(0, 10).ToString()); // 0 to 9 inclusive
}
}
return Convert.ToInt64(sb.ToString());
}
else
{
throw new Exception("Invalid Number Length.  Must be between 1 and 18.");
}
}
``````
0

Featured Post

Tackle projects and never again get stuck behind a technical roadblock.