花了一根烟的时间写了一个随机排列算法,测了一下,效果好象还不错,嘻嘻,没详细测过。好象有很复杂的算法,高手路过贴给我看看。

using System;
using System.Text;

namespace RandomRang
{
 /// <summary>
 /// RandomRange 的摘要说明。
 /// </summary>
 public class RandomRange
 {
  private int range = 0;
  private int [] container;
  private Random rand;

  public RandomRange(int range)
  {
   this.range = range;
   container = new int[range];
   InitContainer();
   long time = DateTime.Now.Ticks;
   string temp = Convert.ToString(time,2).PadLeft(64,'0');
   string temp2 = temp.Substring(32,32);
   rand = new Random(Convert.ToInt32(temp2,2));
  }

  private void InitContainer()
  {
   for(int i=0;i<range;i++)
   {
    container[i] = i+1;
   }
  }

  public void DoRange()
  {
   int j = 0;
   for(int i=0;i<range;i++)
   {
    j = rand.Next(range);
    Switch(i,j);
   }
  }

  private void Switch(int i,int j)
  {
   int temp = 0;
   temp = container[i];
   container[i] = container[j];
   container[j] = temp;
  }

  public override string ToString()
  {
   StringBuilder sb = new StringBuilder();
   for(int i=0;i<range;i++)
   {
    sb.Append(container[i].ToString());
    sb.Append("  ");
   }
   return sb.ToString();
  }
 }
}



using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace RandomRang
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.RichTextBox richTextBox1;
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Button button2;
  private RandomRange rangeMachine;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1(string range)
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   string [] list = new string[1];
   list[0] = range;
   this.label1.Text = String.Format(this.label1.Text,list);
   this.rangeMachine = new RandomRange(Convert.ToInt32(range));
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.richTextBox1 = new System.Windows.Forms.RichTextBox();
   this.button1 = new System.Windows.Forms.Button();
   this.label1 = new System.Windows.Forms.Label();
   this.button2 = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // richTextBox1
   //
   this.richTextBox1.Location = new System.Drawing.Point(24, 32);
   this.richTextBox1.Name = "richTextBox1";
   this.richTextBox1.Size = new System.Drawing.Size(240, 96);
   this.richTextBox1.TabIndex = 0;
   this.richTextBox1.Text = "";
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(56, 136);
   this.button1.Name = "button1";
   this.button1.TabIndex = 1;
   this.button1.Text = "排列";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(24, 8);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(152, 23);
   this.label1.TabIndex = 2;
   this.label1.Text = "Random Range(1~{0}):";
   //
   // button2
   //
   this.button2.Location = new System.Drawing.Point(152, 136);
   this.button2.Name = "button2";
   this.button2.TabIndex = 3;
   this.button2.Text = "退出";
   this.button2.Click += new System.EventHandler(this.button2_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(292, 165);
   this.Controls.Add(this.button2);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.richTextBox1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form2());
  }

  private void button2_Click(object sender, System.EventArgs e)
  {
   Application.Exit();
  }

  private void button1_Click(object sender, System.EventArgs e)
  {
   rangeMachine.DoRange();
   this.richTextBox1.Text = rangeMachine.ToString();
  }
 }
}


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace RandomRang
{
 /// <summary>
 /// Form2 的摘要说明。
 /// </summary>
 public class Form2 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Button button1;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form2()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if(components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.label1 = new System.Windows.Forms.Label();
   this.button1 = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // textBox1
   //
   this.textBox1.Location = new System.Drawing.Point(72, 32);
   this.textBox1.Name = "textBox1";
   this.textBox1.TabIndex = 0;
   this.textBox1.Text = "textBox1";
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(24, 32);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(48, 23);
   this.label1.TabIndex = 1;
   this.label1.Text = "Range:";
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(64, 72);
   this.button1.Name = "button1";
   this.button1.TabIndex = 2;
   this.button1.Text = "OK";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // Form2
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(200, 117);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.textBox1);
   this.Name = "Form2";
   this.Text = "Form2";
   this.ResumeLayout(false);

  }
  #endregion

  private void button1_Click(object sender, System.EventArgs e)
  {
   this.Visible = false;
   new Form1(this.textBox1.Text).Show();
  }
 }
}

posted on 2008-07-26 16:29  恩恩爸爸  阅读(557)  评论(0)    收藏  举报