2014年12月12日 星期五

隨機亂數不重複C#







----------------------------------------------------------------------------------------------




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 WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        Button[,] buttons = new Button[10, 10];宣告一個10乘10的按鈕
        int[] array = new int[10];宣告有1到10的變數
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Instantiating all the buttons in the array



            for (int i = 1; i < 10; i++)
            {
                array[i] = i;
            }



            for (int j = 1; j < 10; j++)
            {
                buttons[1, j] = new Button();
                buttons[1, j].Location = new Point(50*j, 50);
                buttons[1, j].Text = array[j].ToString();
                this.Controls.Add(buttons[1, j]);
            }


            /*
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    buttons[i, j] = new Button();
                    buttons[i, j].Location = new Point(i * 50, j * 50);
                    buttons[i,j].Text = j.ToString();
                    this.Controls.Add(buttons[i, j]);
                }
            }

            */

        }

        private void button1_Click(object sender, EventArgs e)
        {
         int d1,tmp,i;

            Random irand = new Random();
            d1 = irand.Next(1, 10);
            label1.Text = d1.ToString();


            for (int j = 1; j < 10; j++)
            {
                i = 9 - j + 1;
                tmp = array[d1];
                array[d1] = array[i];
                array[i] = tmp;
            }

            //buttons[1, 1].Text = array[1].ToString();

         
            for (int j = 1; j < 10; j++)
            {
            //    buttons[1, j] = new Button();
                buttons[1, j].Location = new Point(50 * j, 50);
                buttons[1, j].Text = array[j].ToString();
                this.Controls.Add(buttons[1, j]);
            }
         

           label2.Text= array[d1].ToString();

        }
    }
}

2014年12月11日 星期四

EXCEL亂數換色(1到9)









----------------------------------------------------------------------------------------------






_Private Sub CommandButton1_Click()


For i = 1 To 9
Cells(1, i) = i
Cells(1, 1).Font.ColorIndex = 0

Next
myrnd = (Fix(Rnd() * 9) + 1)

Cells(3, 3) = myrnd

Cells(1, myrnd).Font.ColorIndex = 5

tmp = Cells(1, myrnd)

Cells(1, myrnd) = Cells(1, 9)

Cells(1, 9) = tmp

myrnd = (Fix(Rnd() * 8) + 1)

tmp = Cells(1, myrnd)

Cells(1, myrnd) = Cells(1, 8)


Cells(1, 8) = tmp


End Sub