關於我自己

我的相片
Welcome to discuss about : Chinese Traditional Medicine and Acupuncture Please send me the email: tccnchsu@gmail.com Chih-Yu Hsu

最新消息

總網頁瀏覽量

2009年12月23日 星期三

隨機亂數


Random rd=new Random();
rd.Next(0,8);
/*
參考
rd.NextDouble();
rd.Next(1);
rd.Next();
*/








using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        // System.Windows.Forms.Button Button1;



        System.Windows.Forms.Button[] Buttons;



        public Form1()
        {

            InitializeComponent();

        }



        private void Form1_Load(object sender, EventArgs e)
        {

            // Button1 = new System.Windows.Forms.Button();

            // this.Controls.Add(Button1);

            int[] score = new int[9];


            Buttons = new System.Windows.Forms.Button[9];



            for (int i = 0; i < 9; ++i)
            {

                Buttons[i] = new Button();

                this.Controls.Add(Buttons[i]);

                if (i <= 2)

                    Buttons[i].Location = new System.Drawing.Point(10 + i * 80, 10);

                else if (i > 2 && i <= 5)
                    Buttons[i].Location = new System.Drawing.Point(10 + (i - 3) * 80, 40);
                else if (i > 5 && i <= 9)
                    Buttons[i].Location = new System.Drawing.Point(10 + (i - 6) * 80, 70);

                
                //score[i]=rd.Next(0, 8);
                score[i] = i;
                
               

            }
            int temp = 0;
            int rdno = 0;
            Random rd = new Random();
            //  不重覆的亂數
            for (int i = 8; i >= 0; --i)
            {
                rdno = rd.Next(0, i);
                temp = score[rdno];
                score[rdno] = score[i];
                score[i] = temp;
            }
            





            for (int i = 0; i < 9; ++i)
            {
               
                Buttons[i].Text = Convert.ToString(score[i]);
            }   
        }
    }
}

沒有留言: