在26个字母中选取任意个数(比如 6)组成新的(600的话)字串,如何用random实现呢..对它不是很熟悉谢谢了.网上的一些贴子看了,但是还是有些不懂.请给出程序段...非常感谢,我会继续努力.

解决方案 »

  1.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace RandomInt
    {
       /// <summary>
       /// Summary description for Form1.
       /// </summary>
       public class RandomInt : System.Windows.Forms.Form
       {
          private System.Windows.Forms.Button showOutputButton;
          private System.Windows.Forms.Label outputLabel;
          /// <summary>
          /// Required designer variable.
          /// </summary>
          private System.ComponentModel.Container components = null;      public RandomInt()
          {
             //
             // Required for Windows Form Designer support
             //
             InitializeComponent();         //
             // TODO: Add any constructor code after InitializeComponent call
             //
          }      /// <summary>
          /// Clean up any resources being used.
          /// </summary>
          protected override void Dispose( bool disposing )
          {
             if( disposing )
             {
                if (components != null) 
                {
                   components.Dispose();
                }
             }
             base.Dispose( disposing );
          }      #region Windows Form Designer generated code
          /// <summary>
          /// Required method for Designer support - do not modify
          /// the contents of this method with the code editor.
          /// </summary>
          private void InitializeComponent()
          {
             this.showOutputButton = new System.Windows.Forms.Button();
             this.outputLabel = new System.Windows.Forms.Label();
             this.SuspendLayout();
             // 
             // showOutputButton
             // 
             this.showOutputButton.Location = new System.Drawing.Point(101, 16);
             this.showOutputButton.Name = "showOutputButton";
             this.showOutputButton.Size = new System.Drawing.Size(91, 23);
             this.showOutputButton.TabIndex = 0;
             this.showOutputButton.Text = "Show Output";
             this.showOutputButton.Click += new System.EventHandler(this.showOutputButton_Click);
             // 
             // outputLabel
             // 
             this.outputLabel.Location = new System.Drawing.Point(14, 56);
             this.outputLabel.Name = "outputLabel";
             this.outputLabel.Size = new System.Drawing.Size(264, 64);
             this.outputLabel.TabIndex = 1;
             // 
             // RandomInt
             // 
             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
             this.ClientSize = new System.Drawing.Size(292, 125);
             this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.outputLabel,
                                                                          this.showOutputButton});
             this.Name = "RandomInt";
             this.Text = "RandomInt";
             this.ResumeLayout(false);      }
          #endregion      /// <summary>
          /// The main entry point for the application.
          /// </summary>
          [STAThread]
          static void Main() 
          {
             Application.Run(new RandomInt());
          }      private void showOutputButton_Click(object sender, System.EventArgs e)
          {
             Random randomInteger = new Random();         outputLabel.Text = "";         // loop 20 times
             for ( int counter = 1; counter <= 20; counter++ )
             {
                // pick random integer between 1 and 6
                int nextValue = randomInteger.Next( 1, 7 );
                
                outputLabel.Text += nextValue + "   "; // append value to output            // add newline after every 5 values
                if ( counter % 5 == 0 )
                   outputLabel.Text += "\n";
             }
          }
       }
    }
      

  2.   

    楼上的不用这么长吧,汉……我晕……首先用Random取数,
    再用substring来截取,
    很简单,建议搂主试着写写,
    有很大帮助
      

  3.   

    using System;public class test
    {
    static void Main()
    {
    for(int i = 0; i < 10; i++)
    {
    Console.WriteLine(Rand(6));
    }
    } static string map = "abcdefghijklmnopqrstuvwxyz";
    static Random rnd = new Random(unchecked((int)DateTime.Now.Ticks));
    public static string Rand(int len)
    {
    char [] data = new char[len];
    for(int i = 0; i < len; i++)
    {
    data[i] = map[rnd.Next() % map.Length];
    }
    return new String(data);
    }
    }
      

  4.   

    不好意思,上面的这个程序的结果是数字而且是单个的.
    我希望的结果是:
                                               
    aa ab ac....ba bb bc....za zb zc....
    aaa aab aac ....
    aba abb abc ....当然上面显示的是顺序的,如果是随机生成的话就应该是另外一回事情了。 所以,我想我的问题还是没得到解决.for  -----这个控制生成的个数
    for -----这个控制随机数的位数(比如:bsadf 是五)
    ........----------这个部分的我就不懂了.
      

  5.   

    产生62位内任意数字大小写字母的随机数 
    private static char[] constant=
      {
       '0','1','2','3','4','5','6','7','8','9',
       'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
       'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
      };
      public static string GenerateRandom(int Length)
      {   
       System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62);
       Random rd= new Random();
       for(int i=0;i<Length;i++)
       {
        newRandom.Append(constant[rd.Next(62)]);
       }
       return newRandom.ToString();
      }
      

  6.   

    要是只要字母,改为如下: 
    private static char[] constant=
      {
       'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
       'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
      };
      public static string GenerateRandom(int Length)
      {   
       System.Text.StringBuilder newRandom = new System.Text.StringBuilder(52);
       Random rd= new Random();
       for(int i=0;i<Length;i++)
       {
        newRandom.Append(constant[rd.Next(52)]);
       }
       return newRandom.ToString();
      }//调用---------------
    sting str=GenerateRandom(6);
      

  7.   

    看一眼:   http://www.sz3000.com/index.htm?QQ=822982