用法 RndString(xx) xx為隨機(jī)數(shù)字長(zhǎng)度
創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的嘉興網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
比如12位長(zhǎng)度的隨機(jī)字符串 則為 RndString(12)
字符串包含數(shù)字和大小寫字母,函數(shù)如下:
Function RndString(ByVal l As Integer) As String
Randomize()
Dim R, i As Integer
Dim S As Char
Dim returnStr As String
returnStr = ""
'a-z 的ASCII碼是:97-122
'A-Z 的ASCII碼是:65-90
'0-9 的ASCII碼是:48-57
For i = 1 To l
R = Int(Rnd() * 62) '隨機(jī)生成的字符有大小寫字母和數(shù)字,共有26個(gè)
If R 10 Then '如果小于10,則是數(shù)字 數(shù)字的ASCII是48-57 對(duì)應(yīng) 隨機(jī)數(shù)字 0-9 所以要將隨機(jī)數(shù)字加48
S = Chr(R + 48) 'Chr 是將把數(shù)字按Ascii碼轉(zhuǎn)換為對(duì)應(yīng)的字符
ElseIf R 36 Then '如果小于36,則是大寫字母 大寫字母的ASCII是65-90 對(duì)應(yīng) 隨機(jī)數(shù)字10-35 所以要將隨機(jī)數(shù)字加55
S = Chr(R + 55)
Else '如果大于36,則是小寫字母 小寫字母的ASCII是97-122 對(duì)應(yīng) 隨機(jī)數(shù)字36-62 所以要將隨機(jī)數(shù)字加61
S = Chr(R + 61)
End If
returnStr = returnStr + S
Next
RndString = returnStr
End Function
整一個(gè)隨機(jī)數(shù),然后把這個(gè)隨機(jī)數(shù)放進(jìn)textbox里邊就可以了,如果需要隨機(jī)函數(shù)網(wǎng)上很多源碼
Dim n As Integer, b As Integer, a(13) As Integer
Private Sub Command1_Click()
If b 5 Then
b = b + 1
x = Int(n * Rnd()) + 1
Text1.Text = Text1.Text a(x) " "
a(x) = a(n)
n = n - 1
Else
Call Form_Load
End If
End Sub
Private Sub Form_Load()
Randomize
n = 13: b = 0
For i = 1 To 13
a(i) = i
Next i
Text1.Text = ""
End Sub
//太蛋疼了,我沒注意到是vb.net,寫成C#的了。。。。
//也許不是太完美,但功能上還是實(shí)現(xiàn)了
//protected void Timer1_Tick(object sender, EventArgs e)
//{
// Random rnd = new Random((int)DateTime.Now.Ticks);
// double result = rnd.NextDouble() * 3.0;
// if (result 1.1)
// {
// result += 1.1;
// }
// Label1.Text = string.Format("{0:0.0}", result);
//}
'下面這個(gè)就是VB.NET的了
Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Randomize()
Dim result As Single = Rnd() * 3.0
If (result 1.1) Then
result += 1.1
End If
Label1.Text = String.Format("{0:0.0}", result)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim HzAsc1 As Integer, HzAsc2 As Integer
Randomize()
HzAsc1 = Int(Rnd() * 10) + 127
HzAsc2 = Int(Rnd() * 10) + 127
TextBox1.Text = ChrW(HzAsc1 * 256 + HzAsc2)
End Sub