I'm trying to create a large array of random numbers using LINQ.
I want to generate 1,000,000 numbers ranging from 1 - 2147483647.
The following code works well for small numbers:
int[] numbers = Enumerable.Range(1, 2147483647)
.OrderBy(x => rnd.Next())
.Take(num)
.ToArray();
But produces a System.OutOfMemory exception when trying to generate a large array.
What's the best way to achieve what I am looking for?
Edit: Thanks for the help so far, I'll write why I'm doing this and my full program code:
Regarding the array, it should not contain duplicates.
I am writing a program that will iterate through all the numbers, pair them up and return the pair with the smallest difference between them. Or return a list of all pairs with the smallest difference if their are duplicates.
Full program code:
static void Main(string[] args)
{
// Keep running until close
while (true)
{
Console.WriteLine("Write a number:");
Console.WriteLine(ClosestNumbers(Convert.ToInt32(Console.ReadLine())));
}
}
public static string ClosestNumbers(int num)
{
string returnString = "\n\nRandom numbers:\n";
returnString += "---------------------------------------\n";
Random rnd = new Random();
// Generate array of {num} random numbers ranging from 1 to 2147483647.
int[] numbers = Enumerable.Range(1, 1000000)
.OrderBy(x => rnd.Next(1, 2147483647))
.Take(num)
.ToArray();
//returnString += string.Join(",", numbers.ToArray()) + "\n";
// Array format: {num1, num2, difference}
List<int[]> pairedDifferences = new List<int[]>();
int endPoint = numbers.Length;
int difference = 0;
for (int i = 0; i < endPoint - 1; i++)
{
for (int a = i + 1; a < endPoint; a++)
{
if (numbers[i] > numbers[a])
{
difference = numbers[i] - numbers[a];
}
else
{
difference = numbers[a] - numbers[i];
}
pairedDifferences.Add(new int[] { numbers[i], numbers[a], difference });
}
}
int minDiff = pairedDifferences.Min(x => x[2]);
List<int[]> minDiffsList = pairedDifferences.Where(x => x[2] == minDiff).ToList();
returnString += "---------------------------------------\n\n\n";
returnString += "Smallest difference(s) found between:\n\n";
foreach (int[] minDiffItem in minDiffsList)
{
// minDiffItem[0]; // first num
// minDiffItem[1]; // second num
// minDiffItem[2]; // difference
returnString += $"{minDiffItem[0]} and {minDiffItem[1]}, with a difference of {minDiffItem[2]}.\n";
}
returnString += "\n\n\n===================================================================\n";
returnString += "===================================================================\n\n\n";
return returnString;
}
Edit 2:
I'm now getting another OutOfMemory exception at the
pairedDifferences.Add(new int[] { numbers[i], numbers[a], difference });
line. Does anyone know a better way to do this? Sorry this is the first time I'm doing something like this.