现在的位置: 首页 > 综合 > 正文

asp.net 随机生成数字,asp.net随机生成字母数字

2013年01月17日 ⁄ 综合 ⁄ 共 3376字 ⁄ 字号 评论关闭
C#代码
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Text;   
  4.   
  5. namespace NetCMS.Common   
  6. {   
  7.     public class Rand   
  8.     {   
  9.         /// <summary>   
  10.         /// 生成随机数字   
  11.         /// </summary>   
  12.         /// <param name="length">生成长度</param>   
  13.         /// <returns></returns>   
  14.         public static string Number(int Length)   
  15.         {   
  16.             return Number(Length, false);   
  17.         }   
  18.   
  19.         /// <summary>   
  20.         /// 生成随机数字   
  21.         /// </summary>   
  22.         /// <param name="Length">生成长度</param>   
  23.         /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>   
  24.         /// <returns></returns>   
  25.         public static string Number(int Length,bool Sleep)   
  26.         {   
  27.             if(Sleep)   
  28.                 System.Threading.Thread.Sleep(3);   
  29.             string result = "";   
  30.             System.Random random = new Random();   
  31.             for (int i = 0; i < Length; i++)   
  32.             {   
  33.                 result += random.Next(10).ToString();   
  34.             }   
  35.             return result;   
  36.         }   
  37.   
  38.         /// <summary>   
  39.         /// 生成随机字母与数字   
  40.         /// </summary>   
  41.         /// <param name="IntStr">生成长度</param>   
  42.         /// <returns></returns>   
  43.         public static string Str(int Length)   
  44.         {   
  45.             return Str(Length, false);   
  46.         }   
  47.         /// <summary>   
  48.         /// 生成随机字母与数字   
  49.         /// </summary>   
  50.         /// <param name="Length">生成长度</param>   
  51.         /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>   
  52.         /// <returns></returns>   
  53.         public static string Str(int Length, bool Sleep)   
  54.         {   
  55.             if(Sleep)   
  56.                 System.Threading.Thread.Sleep(3);   
  57.             char[] Pattern = new char[] { '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' };   
  58.             string result = "";   
  59.             int n = Pattern.Length;   
  60.             System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));   
  61.             for (int i = 0; i < Length; i++)   
  62.             {   
  63.                 int rnd = random.Next(0,n);   
  64.                 result += Pattern[rnd];   
  65.             }   
  66.             return result;   
  67.         }   
  68.   
  69.   
  70.         /// <summary>   
  71.         /// 生成随机纯字母随机数   
  72.         /// </summary>   
  73.         /// <param name="IntStr">生成长度</param>   
  74.         /// <returns></returns>   
  75.         public static string Str_char(int Length)   
  76.         {   
  77.             return Str_char(Length, false);   
  78.         }   
  79.   
  80.         /// <summary>   
  81.         /// 生成随机纯字母随机数   
  82.         /// </summary>   
  83.         /// <param name="Length">生成长度</param>   
  84.         /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>   
  85.         /// <returns></returns>   
  86.         public static string Str_char(int Length, bool Sleep)   
  87.         {   
  88.             if (Sleep)   
  89.                 System.Threading.Thread.Sleep(3);   
  90.             char[] Pattern = new char[] { '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' };   
  91.             string result = "";   
  92.             int n = Pattern.Length;   
  93.             System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));   
  94.             for (int i = 0; i < Length; i++)   
  95.             {   
  96.                 int rnd = random.Next(0, n);   
  97.                 result += Pattern[rnd];   
  98.             }   
  99.             return result;   
  100.         }   
  101.     }   
  102. }  
【上篇】
【下篇】

抱歉!评论已关闭.