zoukankan      html  css  js  c++  java
  • 生成10位由大小写字母和数字组成的随机激活码

    /*
    select char(65+ceiling(rand()*25))   --随机字母(大写)
    select char(97+ceiling(rand()*25))   --随机字母(小写)
    select cast(ceiling(rand()*9) as varchar(1))   --随机数字 1至9的随机数字(整数)
    */
    
    DECLARE  @i    int           
    DECLARE  @flag int
    DECLARE  @SerialNumber  nvarchar(20)
    
    --初始化设定
    SET  @i=1
    SET  @SerialNumber = ''
    
    --生成10位随机码
    WHILE @i<11
    BEGIN
        --设置随机,这个随机会选择字母(大小写)还是数字
        SET @flag=ceiling(rand()*3) 
    
        IF @flag=1 
        BEGIN
           --随机字母(大写)
           select @SerialNumber=@SerialNumber+char(65+ceiling(rand()*25))
        END
        else if @flag=2
             begin
                 --随机字母(小写)
                 select @SerialNumber=@SerialNumber+char(97+ceiling(rand()*25))
             end
             else begin
                 --随机数字 1至9的随机数字(整数)
                 select @SerialNumber=@SerialNumber+cast(ceiling(rand()*9) as varchar
    
    (1))
             end
    
         --进行下一个循环
         SET @i=@i+1
    END
    
    SELECT @SerialNumber as '生成的10位激活码'

    ---但是有的时候需要前面几位必须是纯英文字母,后面几位纯数字或随意字母和数字

    /*
    select char(65+ceiling(rand()*25))   --随机字母(大写)
    select char(97+ceiling(rand()*25))   --随机字母(小写)
    select cast(ceiling(rand()*9) as varchar(1))   --随机数字 1至9的随机数字(整数)
    */
    
    DECLARE  @i    int           
    DECLARE  @flag INT
    DECLARE  @SerialNumber nvarchar(20)
    
    --初始化设定
    SET  @i=1
    SET  @SerialNumber = ''
    
    --生成10位随机码
    WHILE @i<11
    BEGIN
        --设置随机,这个随机会选择字母(大小写)还是数字
        SET @flag=ceiling(rand()*3) 
    
        IF @i < 6  --前五位是字母
        BEGIN
                IF @flag=1 
                BEGIN
                     --随机字母(大写)
                     select @SerialNumber=@SerialNumber+char(65+ceiling(rand()*25))
                END
                ELSE BEGIN
                     --随机字母(小写)
                     select @SerialNumber=@SerialNumber+char(97+ceiling(rand()*25))
                END
    
         END
         ELSE BEGIN  --后面几位是字母或数字
                IF @flag=1 
                BEGIN
                   --随机字母(大写)
                   select @SerialNumber=@SerialNumber+char(65+ceiling(rand()*25))
                END
                else if @flag=2
                     begin
                         --随机字母(小写)
                         select @SerialNumber=@SerialNumber+char(97+ceiling(rand()*25))
                     end
                     else begin
                         --随机数字 1至9的随机数字(整数)
                         select @SerialNumber=@SerialNumber+cast(ceiling(rand()*9) as varchar(1))
                     end
         END
         
         
         --进行下一个循环
         SET @i=@i+1
    END
    
    SELECT @SerialNumber as '生成的10位激活码'
  • 相关阅读:
    为什么有人说指针是 C 语言的精髓?
    属于编程的黄金时代结束了吗?不,这片领地的大门仍然敞开
    编程和编程语言竟然不是一回事,你知道吗?
    为什么 C 语言是程序员的首选,你知道吗?
    CRoundButton2 -一个花哨的图形按钮
    彩虹按钮
    EnableGroupboxControls -一个非mfc函数,用于启用或禁用groupbox中的所有控件
    CImageButtonWithStyle -按钮使用图像与XP视觉风格
    使用。net SDK编写位图按钮控件
    Joe的自动重复按钮类的。net端口
  • 原文地址:https://www.cnblogs.com/DBArtist/p/ActivationCode.html
Copyright © 2011-2022 走看看