using System;
using System. Data;
using System. Linq;
using System. Windows. Forms;
namespace 随机产生字符串
{
public partial class Form1 : Form
{
public Form1 ( )
{
InitializeComponent ( ) ;
}
Random random = new Random ( ) ;
private string chars = "ABCDEFGHIJKLMNOPQRSTUWVXYZ0123456789abcdefghijklmnopqrstuvwxyz" ;
private void button1_Click ( object sender, EventArgs e)
{
this . textBox1. Clear ( ) ;
this . textBox1. AppendText ( radomstrs ( chars, 8 ) ) ;
}
private string radomstrs ( string chars , int length)
{
string strs = string . Empty;
for ( int i = 0 ; i < length; i++ )
{
strs + = chars[ random. Next ( chars. Length) ] ;
}
return strs;
}
private void button2_Click ( object sender, EventArgs e)
{
this . textBox1. Clear ( ) ;
this . textBox1. AppendText ( radomstrsbyling ( chars, 8 ) ) ;
}
private string radomstrsbyling ( string chars, int length)
{
return new string ( Enumerable. Repeat ( chars, length) . Select ( s = > s[ random. Next ( chars. Length) ] ) . ToArray ( ) ) ;
}
}
}
Random random = new Random ( ) ;
byte [ ] bytes = new byte [ random. Next ( 0 , 10000 ) ] ;
System. Security. Cryptography. RNGCryptoServiceProvider rNGCryptoServiceProvider = new System. Security. Cryptography. RNGCryptoServiceProvider ( ) ;
rNGCryptoServiceProvider. GetBytes ( bytes) ;
int num = BitConverter. ToInt32 ( bytes, 0 ) ;
private static Random random = new Random ( ) ;
public string GetRandomStr ( string chars, int length)
{
if ( string . IsNullOrEmpty ( chars) )
{
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghizklmnopqrstuvwxyz0123456789" ;
}
return new string ( Enumerable. Repeat ( chars, length)
. Select ( s = > s[ random. Next ( s. Length) ] ) . ToArray ( ) ) ;
}