random.nextdouble

Hello,

i have trouble using the rand.NextDouble()

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

Open in new window


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

Open in new window


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

Open in new window

it works

why is this ? can someone please help me
thank you
kensy11Asked:
Who is Participating?
 
gplanaCommented:
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
 
gplanaCommented:
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
 
gplanaCommented:
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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

using rand.Next(,); or something similar to it
0
 
käµfm³d 👽Commented:
How about:


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

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

Open in new window


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
 
käµfm³d 👽Commented:
Actually, I think I'm not 100% correct on the above.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If that hurts your head too much, then do something like:
        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.");
            }
        }

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.