I wrote this Program to rearrange 1,2,...,16 randomly:
using System;
class Program
{
static Random ran = new Random();
static int[] Select()
{
int[] a = {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0};
int[] b = {1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1};
for(int i = 0; i < a.Length; i++)
{
a[i] = ran.Next(a.Length - i);
for(int j = 0; j <= a[i]; j++)
{
if(b[j] == 0)
a[i]++;
}
b[a[i]] = 0;
a[i]++; // just because a[i] is in {0,...,15} and must be in {1,..,16}
}
return a;
}
static void Main()
{
int[] a = Select();
for(uint i = 0; i < a.Length; i++)
Console.WriteLine(a[i]);
/// test:
/*int[] test = {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0};
for(uint i = 0; i < 16000; i++)
{
int[] a = Select();
test[a[6] - 1]++;
}
for(uint i = 0; i < 16; i++)
Console.WriteLine(test[i]);*/
Console.ReadKey();
}
}
Do you have any idea how to improve it. Is there anything wrong in my program?


Sign In
Create Account


Back to top









