Random rnd = new Random();
Int64 i64 = rnd.Next(1000000, 9999999);
i64 = (i64*1000000)+rnd.Next(0, 999999999);
From Microsoft Documentation:
The overloads of the Next method return 32-bit integers. However, in some cases, you might want to work with 64-bit integers. You can do this as follows:
1. Call the NextDouble method to retrieve a double-precision floating point value.
2. Multiply that value by Int64.MaxValue.
The following example uses this technique to generate 20 million random long integers and categorizes them in 10 equal groups. It then evaluates the distribution of the random numbers by counting the number in each group from 0 to Int64.MaxValue. As the output from the example shows, the numbers are distributed more or less equally through the range of a long integer.
const long ONE_TENTH = 922337203685477581;
Random rnd = new Random();
long number;
int[] count = new int[10];
// Generate 20 million long integers.
for (int ctr = 1; ctr <= 20000000; ctr++) {
number = (long) (rnd.NextDouble() * Int64.MaxValue);
// Categorize random numbers.
count[(int) (number / ONE_TENTH)]++;
}
// Display breakdown by range.
Console.WriteLine("{0,28} {1,32} {2,7}\n", "Range", "Count", "Pct.");
for (int ctr = 0; ctr <= 9; ctr++)
Console.WriteLine("{0,25:N0}-{1,25:N 0} {2,8:N0} {3,7:P2}", ctr * ONE_TENTH,
ctr < 9 ? ctr * ONE_TENTH + ONE_TENTH - 1 : Int64.MaxValue,
count[ctr], count[ctr]/20000000.0);
The results of the above code
// The example displays output like the following:
// Range Count Pct.
//
// 0- 922,337,203,685,477,580 1,996,148 9.98 %
// 922,337,203,685,477,581-1,844,674,40 7,370,955, 161 2,000,293 10.00 %
// 1,844,674,407,370,955,162-2,767,011, 611,056,43 2,742 2,000,094 10.00 %
// 2,767,011,611,056,432,743-3,689,348, 814,741,91 0,323 2,000,159 10.00 %
// 3,689,348,814,741,910,324-4,611,686, 018,427,38 7,904 1,999,552 10.00 %
// 4,611,686,018,427,387,905-5,534,023, 222,112,86 5,485 1,998,248 9.99 %
// 5,534,023,222,112,865,486-6,456,360, 425,798,34 3,066 2,000,696 10.00 %
// 6,456,360,425,798,343,067-7,378,697, 629,483,82 0,647 2,001,637 10.01 %
// 7,378,697,629,483,820,648-8,301,034, 833,169,29 8,228 2,002,870 10.01 %
// 8,301,034,833,169,298,229-9,223,372, 036,854,77 5,807 2,000,303 10.00 %
const ulong minLong = 1000000000000000;
const ulong maxLong = 9999999999999999;
Random rnd = new Random();
ulong number;
do
{
number = (ulong)(rnd.NextDouble() * maxLong);
} while (!(number >= minLong && number <= maxLong));
how can I fix the result to 16 digits always?
Int64 i64 = rnd.Next(1000000, 9999999); // this makes the number to have 7 digits
but very unique almost certainly unique, much more unique then any of the solutions provideda GUID is a 128 bit number in the range of 0 to 2^128 what is about 3.4 x 10^38. that means the number has at least 37 decimal digits in the range from 0 to 9 each. if you reduce this to 16 digits you will use only the 10^21-th part of this pool what is still a huge number of more than 10 thousand trillion numbers, but any considerations about the uniqueness of the original guid number are out of question.
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
I need help displaying 2 models in one view in my MVC.Net application with C# code behind | 5 | 51 | |
Entity Framework | 7 | 43 | |
MediaHelp | 4 | 23 | |
insert value of checklistbox checked | 4 | 27 |
Join the community of 500,000 technology professionals and ask your questions.