基本信息
源码名称:C# 超强生成随机数 实例源码
源码大小:0.07M
文件格式:.rar
开发语言:C#
更新时间:2014-06-11
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Diagnostics;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class 系统唯一数 : Form
{
public 系统唯一数()
{
InitializeComponent();
}
private void btnCreate_Click(object sender, EventArgs e)
{
this.UseWaitCursor = true;
Stopwatch sw = new Stopwatch();
sw.Start();
List<string> list = GetRandString((int)numLen.Value, (int)numCount.Value);
sw.Stop();
this.UseWaitCursor = false;
if (list == null)
{
return;
}
string strInfo = string.Format("成功生成{1}个随机字符串!用时:{0}毫秒!", sw.ElapsedMilliseconds, numCount.Value);
strInfo = "\r\n" "是否查看生成结果?";
DialogResult result = MessageBox.Show(strInfo, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (result == DialogResult.OK)
{
StringBuilder str_content = new StringBuilder();
foreach (string r in list)
{
str_content.Append(r "\r\n");
}
string file_path = Application.StartupPath "\\rand_" DateTime.Now.ToFileTime().ToString() ".txt";
File.WriteAllText(file_path, str_content.ToString());
Process.Start(file_path);
}
}
/// <summary>
/// 随机排序
/// </summary>
/// <param name="charList"></param>
/// <returns></returns>
private List<string> SortByRandom(List<string> charList)
{
Random rand = new Random();
for (int i = 0; i < charList.Count; i )
{
int index = rand.Next(0, charList.Count);
string temp = charList[i];
charList[i] = charList[index];
charList[index] = temp;
}
return charList;
}
private void ShowError(string strError)
{
MessageBox.Show(strError, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
/// <summary>
/// 获取随机字符串
/// </summary>
/// <param name="len"></param>
/// <param name="count"></param>
/// <returns></returns>
private List<string> GetRandString(int len, int count)
{
double max_value = Math.Pow(36, len);
if (max_value > long.MaxValue)
{
ShowError(string.Format("Math.Pow(36, {0}) 超出 long最大值!", len));
return null;
}
long all_count = (long)max_value;
long stepLong = all_count / count;
if (stepLong > int.MaxValue)
{
ShowError(string.Format("stepLong ({0}) 超出 int最大值!", stepLong));
return null;
}
int step = (int)stepLong;
if (step < 3)
{
ShowError("step 不能小于 3!");
return null;
}
long begin = 0;
List<string> list = new List<string>();
Random rand = new Random();
while (true)
{
long value = rand.Next(1, step) begin;
begin = step;
list.Add(GetChart(len, value));
if (list.Count == count)
{
break;
}
}
list = SortByRandom(list);
return list;
}
//数字 字母
private const string CHAR = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/// <summary>
/// 将数字转化成字符串
/// </summary>
/// <param name="len"></param>
/// <param name="value"></param>
/// <returns></returns>
private string GetChart(int len, long value)
{
StringBuilder str = new StringBuilder();
while (true)
{
str.Append(CHAR[(int)(value % 36)]);
value = value / 36;
if (str.Length == len)
{
break;
}
}
return str.ToString();
}
}
}