1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LotteryCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int j;
            int[] MyArray = new int[6];
            int[] Numbers = new int[49];
            Random rnd = new Random();
            for (j = 0; j < 49; j++)
            {
                Numbers[j] = j + 1;//將Numbers[j]塞入j+1 
            }
                for (j = 0; j < 6; j++)
                {
                    do
                    {
                        int a = rnd.Next(0, 49);
                        MyArray[j] = Numbers[a];
                        Numbers[a] = 0;
                    }
                    while (MyArray[j] == 0);
                    //如果抽到的位置空了(被抽過=0)就再抽
                }
            Array.Sort(MyArray); // 排序MyArray
            for (j = 0; j < 6; j++)
            {
                Console.WriteLine("{0}", MyArray[j]); // 印出MyArray
            }
        }
    }
}