在26个字母中选取任意个数(比如 6)组成新的(600的话)字串,如何用random实现呢..对它不是很熟悉谢谢了.网上的一些贴子看了,但是还是有些不懂.请给出程序段...非常感谢,我会继续努力.
解决方案 »
- C#中datagridview怎么获得选中的多个行的绑定到该行的ID或者索引?
- 嘿嘿嘿,ASP.NET(C#)学习群,欢迎加入,共同学习进步。
- 怎样用C#得到和Microsoft的控制面板中“添加或删除程序”里面显示一样的软件列表?
- 简单问题
- 例子程序问题:Update Application Block例子SelfUpdating出错,请帮忙看看??
- 请教一算法问题?希望大家给点思路!
- 推荐大家一个好网站
- Button.Click -= EventHandler( OnClick) 去除按键OnClick事件的方法怎么写?
- 在C#下编写的程序如何在一台没装Visiual Studio.net的机器上运行?
- 有请高手花点时间
- 请教如何把一个文件夹下的所有文件内容读出,文件个数不定,主要是文件个数不定这个问题,怎么判断它还有没有未读的文件?
- 算术表达式生成分析树
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";
}
}
}
}
再用substring来截取,
很简单,建议搂主试着写写,
有很大帮助
{
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);
}
}
我希望的结果是:
aa ab ac....ba bb bc....za zb zc....
aaa aab aac ....
aba abb abc ....当然上面显示的是顺序的,如果是随机生成的话就应该是另外一回事情了。 所以,我想我的问题还是没得到解决.for -----这个控制生成的个数
for -----这个控制随机数的位数(比如:bsadf 是五)
........----------这个部分的我就不懂了.
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();
}
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);