代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
从0到9中随机生成8位数字,同批次的流水码不重复
作者:
Dezai.CN
/ 发布于
2011/7/7
/
551
<div><span style="color: rgb(0,0,255)">private</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">char</span><span style="color: rgb(0,0,0)">[] constant </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> { </span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(128,0,0)">0</span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(128,0,0)">1</span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(128,0,0)">2</span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(128,0,0)">3</span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(128,0,0)">4</span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(128,0,0)">5</span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(128,0,0)">6</span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(128,0,0)">7</span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(128,0,0)">8</span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(128,0,0)">9</span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(0,0,0)"> }; </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)"> Main(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)">[] args) { </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)">[] AllID </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)">[</span><span style="color: rgb(128,0,128)">100</span><span style="color: rgb(0,0,0)">]; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">存放所有已经生成的流水号</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> GenerateRandomNumber(</span><span style="color: rgb(128,0,128)">100</span><span style="color: rgb(0,0,0)">, AllID); </span><span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> i </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; i </span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(0,0,0)"> AllID.Length; i</span><span style="color: rgb(0,0,0)">++</span><span style="color: rgb(0,0,0)">) Console.WriteLine(AllID[i]); Console.ReadLine(); } </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)"> GenerateRandomNumber(</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> Length,</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)">[] P_AllID) </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">Length要的流水号个数,P_AllID存放生成的流水号</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> { Random rd </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> Random(); </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> Num</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">do</span><span style="color: rgb(0,0,0)"> { System.Text.StringBuilder newRandom </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> System.Text.StringBuilder(</span><span style="color: rgb(128,0,128)">8</span><span style="color: rgb(0,0,0)">); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">存放一个流水号</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> i </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; i </span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">8</span><span style="color: rgb(0,0,0)">; i</span><span style="color: rgb(0,0,0)">++</span><span style="color: rgb(0,0,0)">) </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">8位号码,是其他位数,改此即可</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> { newRandom.Append(constant[rd.Next(</span><span style="color: rgb(128,0,128)">10</span><span style="color: rgb(0,0,0)">)]); } </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)">(</span><span style="color: rgb(0,0,0)">!</span><span style="color: rgb(0,0,0)">IsExist(newRandom.ToString(),P_AllID,Num)) </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">如果该号还没生成,就加入数组P_AllID</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> P_AllID[Num</span><span style="color: rgb(0,0,0)">++</span><span style="color: rgb(0,0,0)">] </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> newRandom.ToString(); } </span><span style="color: rgb(0,0,255)">while</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,0)">!</span><span style="color: rgb(0,0,0)">(Num </span><span style="color: rgb(0,0,0)">>=</span><span style="color: rgb(0,0,0)"> Length)); } </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">查找当前生成的流水号是否已经存在</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">bool</span><span style="color: rgb(0,0,0)"> IsExist(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> CurrentID, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)">[] P_AllID, </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> CurrentLength) </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">CurrentLength指:数组P_AllID当前实际元素的个数</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> { </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (CurrentLength </span><span style="color: rgb(0,0,0)">==</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">) </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> x </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; x </span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(0,0,0)"> CurrentLength;x</span><span style="color: rgb(0,0,0)">++</span><span style="color: rgb(0,0,0)">) </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (CurrentID </span><span style="color: rgb(0,0,0)">==</span><span style="color: rgb(0,0,0)"> P_AllID[x]) { </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">; } </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">; } </span></div>
试试其它关键字
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3