偶不太懂随机函数,不知道如何实现以下几个要求:
1.生成随机的小写字母、或小写字母与数字混合
2.生成随机的汉字 请各位帮忙解答,谢谢

解决方案 »

  1.   

    这是我程序里的一段代码,奉献给你了。
    生成随机(单个)字母:Public Function GetRandomChar() As String    On Error Resume Next    Dim TmpResult As String * 1
        Dim tmpNumL As Byte
        Dim tmpNumR As Byte    tmpNumL = 100
        tmpNumR = 68    TmpResult = Chr(tmpNumR)    Randomize CInt(Rnd(tmpNumL) * tmpNumL)
        tmpNumL = CByte(Rnd(CLng(Timer)) * 28 + 96)
        If tmpNumL < 97 Then tmpNumL = 97
        If tmpNumL > 122 Then tmpNumL = 122    Randomize CInt(Rnd(tmpNumR) * tmpNumR)
        tmpNumR = CByte(Rnd(CLng(Timer)) * 28 + 64)
        If tmpNumR < 65 Then tmpNumR = 65
        If tmpNumR > 90 Then tmpNumR = 90    Randomize
        If Rnd(1) < 0.5 Then TmpResult = Chr(tmpNumL) Else TmpResult = Chr(tmpNumR)    GetRandomChar = TmpResultEnd Function
    生成随机(单个)汉字:Private Function GetRandUCChar() As String    On Error Resume Next    Dim TmpRes As String
        Dim TmpRnd As Integer
        Randomize
        If Rnd() < 0.6 Then TmpRes = "&HB" Else TmpRes = "&HC"
        Randomize
        TmpRes = TmpRes & Hex(Rnd(1) * 15)    Randomize
        TmpRnd = CInt(Rnd(1) * 7)    Select Case TmpRnd
            Case 0, 1
                TmpRes = TmpRes & "A"
            Case 2
                TmpRes = TmpRes & "B"
            Case 3
                TmpRes = TmpRes & "C"
            Case 4
                TmpRes = TmpRes & "D"
            Case 5
                TmpRes = TmpRes & "E"
            Case Else
                TmpRes = TmpRes & "F"
        End Select    Randomize
        TmpRes = TmpRes & Hex(Rnd(1) * 15)    GetRandUCChar = Chr(TmpRes)End Function
      

  2.   

    ? Chr(Int(Rnd * (Asc("z") + 1 - Asc("a"))) + Asc("a")) '随机小写字母
    ? Chr(Int(Rnd * (Asc("9") + 1 - Asc("0"))) + Asc("0")) '随机数字
      

  3.   

    谢谢 aaric(aaric) ,不过偶是初学、资质愚钝~~看不大明白你的这段代码
    谢谢 happy_sea(开心海) 你的方法确实简单~ 但是怎样生成多个随机字母或数字呢? 比如生成8个,不会是将代码复制粘贴8次吧,呵呵。另,你有没有生成随机汉字的方法?
      

  4.   

    谢谢 happy_sea(开心海) 你的方法确实简单~ 但是怎样生成多个随机字母或数字呢? 比如生成8个,不会是将代码复制粘贴8次吧,呵呵。另,你有没有生成随机汉字的方法?=============================用一个for next不断调用即可
      

  5.   

    关于随机汉字的生成比较麻烦一些,汉字的ascii码值在-10254到-20319之间,但是这个范围内的字符还有很多是不可显示的(好像是保留的),而且也包括了繁体字。或许用unicode码来实现简单一些,没具体研究过。
      

  6.   

    取得汉字的asc码的功能没做过.不过我这里有一个取得部分汉字的拼音的功能函数
    其中就用到了取得各个拼音首自己的汉字区间.将这个和开心海的代码结合一下就应该可以取得汉字随即的效果.虽然不是最全的汉字随即结果,但是还是应该可以满足你的需求了样本如下,本身我没有vb就不给你改动了◆标题◆    
    取得部分汉字的拼音 
     --------------------------------------------------------------------------------
     
     ◆简介◆    
    取得汉字的拼音首字母 --------------------------------------------------------------------------------
     
     ◆内容◆    
    Public Function GetPY(a1 As String) As String
           '获得输入名称的首字拼音
           Dim t1   As String
           If Asc(a1) < 0 Then
           t1 = Left(a1, 1)
           If Asc(t1) < Asc("啊") Then
           GetPY = "0"
           Exit Function
           End If
           If Asc(t1) >= Asc("啊") And Asc(t1) < Asc("芭") Then
           GetPY = "A"
           Exit Function
           End If
           If Asc(t1) >= Asc("芭") And Asc(t1) < Asc("擦") Then
           GetPY = "B"
           Exit Function
           End If
           If Asc(t1) >= Asc("擦") And Asc(t1) < Asc("搭") Then
           GetPY = "C"
           Exit Function
           End If
           If Asc(t1) >= Asc("搭") And Asc(t1) < Asc("蛾") Then
           GetPY = "D"
           Exit Function
           End If
           If Asc(t1) >= Asc("蛾") And Asc(t1) < Asc("发") Then
           GetPY = "E"
           Exit Function
           End If
           If Asc(t1) >= Asc("发") And Asc(t1) < Asc("噶") Then
           GetPY = "F"
           Exit Function
           End If
           If Asc(t1) >= Asc("噶") And Asc(t1) < Asc("哈") Then
           GetPY = "G"
           Exit Function
           End If
           If Asc(t1) >= Asc("哈") And Asc(t1) < Asc("击") Then
           GetPY = "H"
           Exit Function
           End If
           If Asc(t1) >= Asc("击") And Asc(t1) < Asc("喀") Then
           GetPY = "J"
           Exit Function
           End If
           If Asc(t1) >= Asc("喀") And Asc(t1) < Asc("垃") Then
           GetPY = "K"
           Exit Function
           End If
           If Asc(t1) >= Asc("垃") And Asc(t1) < Asc("妈") Then
           GetPY = "L"
           Exit Function
           End If
           If Asc(t1) >= Asc("妈") And Asc(t1) < Asc("拿") Then
           GetPY = "M"
           Exit Function
           End If
           If Asc(t1) >= Asc("拿") And Asc(t1) < Asc("哦") Then
           GetPY = "N"
           Exit Function
           End If
           If Asc(t1) >= Asc("哦") And Asc(t1) < Asc("啪") Then
           GetPY = "O"
           Exit Function
           End If
           If Asc(t1) >= Asc("啪") And Asc(t1) < Asc("期") Then
           GetPY = "P"
           Exit Function
           End If
           If Asc(t1) >= Asc("期") And Asc(t1) < Asc("然") Then
           GetPY = "Q"
           Exit Function
           End If
           If Asc(t1) >= Asc("然") And Asc(t1) < Asc("撒") Then
           GetPY = "R"
           Exit Function
           End If
           If Asc(t1) >= Asc("撒") And Asc(t1) < Asc("塌") Then
           GetPY = "S"
           Exit Function
           End If
           If Asc(t1) >= Asc("塌") And Asc(t1) < Asc("挖") Then
           GetPY = "T"
           Exit Function
           End If
           If Asc(t1) >= Asc("挖") And Asc(t1) < Asc("昔") Then
           GetPY = "W"
           Exit Function
           End If
           If Asc(t1) >= Asc("昔") And Asc(t1) < Asc("压") Then
           GetPY = "X"
           Exit Function
           End If
           If Asc(t1) >= Asc("压") And Asc(t1) < Asc("匝") Then
           GetPY = "Y"
           Exit Function
           End If
           If Asc(t1) >= Asc("匝") Then
           GetPY = "Z"
           Exit Function
           End If
           Else
           If UCase(a1) <= "Z" And UCase(a1) >= "A" Then
           GetPY = UCase(Left(a1, 1))
           Else
           GetPY = "0"
           End If
           End If
    End Function