SO - Random Crash

CSharp · Console

/Program.cs

using System;
					
public class Program
{
	public static void Main()
	{
		int[] _asteroidPathsArray = new int[5];
		//int[] _asteroidPathsArray = new int[5] {-1,-1,-1,-1,-1};
		for (int i = 0; i < _asteroidPathsArray.Length; i++)
        {
            int path = GetRandom(0, 5);
            while(System.Array.IndexOf(_asteroidPathsArray, path) >= 0)
            {
                path = GetRandom(0, 5);
            }
			Console.WriteLine("i:" + i + " path:" + path);
            _asteroidPathsArray[i] = path;
        }
	}
	static Random r = new Random();
	public static int GetRandom(int start, int end) {
		
        return r.Next(start, end); //for ints
	}
}
;