asp 生成随机码,随机数可自定义长度

 

案例1-随机码

<%
function makePassword(byVal maxLen)
Dim strNewPass
Dim whatsNext, upper, lower, intCounter
Randomize
For intCounter = 1 To maxLen
whatsNext = Int((1 - 0 + 1) * Rnd + 0)
If whatsNext = 0 Then
'character
upper = 90
lower = 65
Else
upper = 57
lower = 48
End If
strNewPass = strNewPass & Chr(Int((upper - lower + 1) * Rnd + lower))
Next
makePassword = strNewPass
end function
%>
<%
'6是长度
%>
<%=makePassword(6)%>

结果

76IZ61

 


案例2-随机码

 <%
'ASP生成随机字符串(数字+大小写字母)练习
'阿会楠练习,为我所用,非我所想 
Function randKey(obj)
 Dim char_array(80)
 Dim temp
 For i = 0 To 9 
  char_array(i) = Cstr(i)
 Next
 For i = 10 To 35
  char_array(i) = Chr(i + 55)
 Next
 For i = 36 To 61
  char_array(i) = Chr(i + 61)
 Next
 Randomize
 For i = 1 To obj
  'rnd函数返回的随机数在0~1之间,可等于0,但不等于1
  '公式:int((上限-下限+1)*Rnd+下限)可取得从下限到上限之间的数,可等于下限但不可等于上限
  temp = temp&char_array(int(62 - 0 + 1)*Rnd + 0)
 Next
 randKey = temp
End Function
Response.Write(randKey(6))
%>

结果

rJR14T


案例3-随机码

<%
Sub StrRandomize(strSeed)
Dim i, nSeed
nSeed = CLng(0)
For i = 1 To Len(strSeed)
nSeed = nSeed Xor ((256 * ((i - 1) Mod 4) * AscB(Mid(strSeed, i, 1))))
Next
Randomize nSeed
End Sub

Function GeneratePassword(nLength)
Dim i, bMadeConsonant, c, nRnd
Const strDoubleConsonants = "bdfglmnpst"
Const strConsonants = "bcdfghklmnpqrstv"
Const strVocal = "aeiou"
GeneratePassword = ""
bMadeConsonant = False
For i = 0 To nLength
nRnd = Rnd
If GeneratePassword <> "" AND (bMadeConsonant <> True) AND (nRnd < 0.15) Then
c = Mid(strDoubleConsonants, Int(Len(strDoubleConsonants) * Rnd + 1), 1)
c = c & c
i = i + 1
bMadeConsonant = True
Else
If (bMadeConsonant <> True) And (nRnd < 0.95) Then
c = Mid(strConsonants, Int(Len(strConsonants) * Rnd + 1), 1)
bMadeConsonant = True
Else
c = Mid(strVocal,Int(Len(strVocal) * Rnd + 1), 1)
bMadeConsonant = False
End If
End If
GeneratePassword = GeneratePassword & c
Next
If Len(GeneratePassword) > nLength Then
GeneratePassword = Left(GeneratePassword, nLength)
End If
End Function
%>

案例3-产生一个六位的密码

<%
'产生一个六位的密码
StrRandomize CStr(Now) & CStr(Rnd)
response.write GeneratePassword(6)
%>

结果:risiha

案例3-产生一个8位的密码

<%
'产生一个8位的密码
StrRandomize CStr(Now) & CStr(Rnd)
response.write GeneratePassword(8)
%>

结果:mepuroqe

案例3-产生一个10位的密码

 <%
'产生一个10位的密码
StrRandomize CStr(Now) & CStr(Rnd)
response.write GeneratePassword(10)
%>

结果:meteguffeb


案例4-【本实例生成的十三位随机字符串为:】

<%
Function gen_key(digits)
'定义并初始化数组
    dim char_array(80)
 '初始化数字
    For i = 0 To 9
        char_array(i) = CStr(i)
    Next
 '初始化大写字母
    For i = 10 To 35
        char_array(i) = Chr(i + 55)
    Next
 '初始化小写字母
    For i = 36 To 61
        char_array(i) = Chr(i + 61)
    Next
 
 do while len(output) < digits
        num = char_array(Int((62 - 0 + 1) * Rnd + 0))
        output = output + num
    loop
'设置返回值
    gen_key    =    output
End Function
'把结果返回给浏览器 
response.write(gen_key(13)) 
%>

结果:H52llYGZBzAXV


案例5-1【ASP生成9位随机数】

<%
'纯随机,不重复 
Response.write Int((999999999 * Rnd) + 111111111) 
'生成111111111到999999999之间随机数
%>

结果:654716497

案例5-2【ASP生成4位随机数】

<%
dim anli5_res2
anli5_res2=int(9000*rnd())+1000
Response.write(anli5_res2)
'生成1000到9000之间随机数
%>

结果:1431

案例5-3【ASP生成6位随机数】

<%
dim anli5_res3
anli5_res3=int(999999*rnd())+111111
Response.write(anli5_res3)
'生成111111到999999之间随机数
%>

结果:436031

案例6【ASP生成的是一个不重复的数组】

<% 
'生成的是一个不重复的数组 
Function GetRnd(lowerNum,upperNum) 
Dim unit,RndNum,Fun_X 
unit = upperNum - lowerNum 
Redim MyArray(unit) 
For Fun_I=0 To unit 
myArray(Fun_I)= lowerNum + Fun_I 
Next 
For Fun_I=0 To round(unit) 
RndNum = getRndNumber(Fun_I,unit) 
Fun_X = myArray(RndNum) 
myArray(RndNum)=myArray(Fun_I) 
myArray(Fun_I)=Fun_X 
Next 
GetRnd = Join(myArray) 
End Function 
Function getRndNumber(lowerbound,upperbound) 
Randomize 
getRndNumber=Int((upperbound-lowerbound+1)*Rnd+lowerbound) 
End Function 
Response.Write GetRnd(1,1000) 
%> 

结果:296 734 156 628 477 403 626 952 242 651 416 597 59 948 520 556 189 570 673 826 4 245 11 427 396 490 925 80 215 783 55 42 343 412 464 310 774 93 918 625 291 335 714 283 367 623 560 22 492 957 960 508 664 893 459 816 440 877 260 729 265 469 359 690 388 798 506 699 805 736 945 320 584 721 747 915 34 72 843 876 533 645 985 211 942 73 500 511 482 18 308 428 554 79 404 392 672 497 545 402 835 438 969 933 621 425 779 613 455 694 871 577 571 354 357 821 966 297 37 227 756 285 588 792 592 550 448 121 706 217 815 995 225 161 115 640 657 150 175 333 725 411 898 299 836 940 100 536 364 882 804 814 787 868 624 88 629 278 713 5 738 66 94 709 275 742 147 981 912 60 120 903 128 977 316 154 863 661 445 28 408 607 832 541 131 595 659 252 879 839 744 221 983 530 869 179 537 160 658 708 935 466 489 634 786 707 362 602 889 95 697 611 451 931 282 41 267 33 900 795 904 579 119 763 228 284 326 976 575 801 691 456 958 992 529 950 253 458 610 917 114 928 728 368 110 676 330 56 139 864 923 548 927 341 255 518 476 802 373 724 212 557 723 964 102 741 567 701 303 962 82 97 400 682 758 872 141 443 569 112 38 155 71 322 422 840 305 295 517 568 606 493 834 334 146 277 480 512 791 475 197 13 678 127 157 103 971 641 755 922 605 866 125 911 461 803 465 614 534 775 16 854 91 782 859 809 51 507 68 162 817 939 229 509 887 314 663 191 989 431 233 268 136 648 627 231 503 589 680 888 573 564 765 218 675 572 138 598 978 953 730 348 842 302 612 324 908 329 695 184 543 873 133 501 286 452 827 292 200 853 712 274 609 828 124 447 195 996 454 409 547 75 759 167 420 531 807 587 9 294 885 254 581 355 555 39 177 870 851 45 134 187 166 98 800 637 818 860 891 247 414 116 585 987 332 812 202 104 183 83 528 909 906 767 618 76 593 361 401 685 372 722 434 487 240 890 825 467 339 679 336 622 684 644 158 230 956 23 653 752 855 232 290 731 27 12 220 737 630 954 516 522 67 558 92 862 201 718 849 841 483 15 549 984 716 867 794 638 972 132 687 967 347 241 266 44 356 8 820 582 395 205 474 878 481 213 236 702 498 130 3 453 137 181 207 819 746 710 471 174 633 429 478 806 391 975 770 153 261 444 844 196 822 494 442 727 159 165 965 352 287 143 281 188 346 149 748 246 894 140 270 386 669 64 486 667 129 552 796 10 766 168 982 223 846 951 460 916 338 944 662 58 666 393 164 418 263 432 777 865 913 881 526 369 152 875 32 551 142 698 930 496 793 163 993 652 583 331 780 785 773 426 590 750 524 932 495 70 858 366 151 413 929 538 424 203 838 754 433 101 768 514 946 122 519 148 170 317 986 705 619 544 542 52 829 914 831 813 771 899 17 677 924 998 683 513 468 380 576 655 665 61 209 762 959 523 934 259 761 185 289 360 36 926 463 14 852 808 226 717 350 559 521 337 106 398 719 219 769 186 117 896 704 963 907 234 740 328 351 276 144 470 778 344 938 635 216 850 874 280 457 941 631 43 994 384 113 374 472 811 421 895 89 171 358 376 643 382 540 325 961 790 505 250 668 199 604 435 178 601 681 591 49 856 251 920 515 757 772 686 857 300 897 40 370 389 25 484 243 81 371 973 535 301 169 272 901 656 600 204 239 50 726 182 749 437 109 603 837 210 311 436 87 546 739 449 696 674 485 910 99 776 700 172 7 703 108 298 608 617 586 504 751 892 430 639 145 955 235 258 580 323 419 649 692 715 525 21 720 176 90 271 180 237 78 553 902 491 349 57 937 980 319 35 318 63 642 566 312 192 884 313 249 264 6 947 824 69 991 883 340 206 654 745 647 886 974 574 735 85 345 561 190 646 921 479 919 365 194 309 84 224 599 410 262 1 383 799 257 406 65 48 46 764 306 441 222 394 193 943 880 636 660 353 111 578 307 845 781 407 949 24 688 126 405 632 539 377 979 1000 473 905 399 327 123 499 671 784 378 86 990 19 397 62 288 753 446 77 563 415 848 988 450 342 733 936 208 379 20 789 615 387 760 315 363 999 135 248 596 279 732 53 273 321 256 118 293 417 788 861 502 510 462 238 562 650 29 689 214 74 269 488 594 423 532 173 96 693 390 743 810 670 375 30 970 968 997 381 385 198 616 105 620 304 711 54 244 47 565 26 31 847 527 833 797 830 823 439 2 107

案例7【ASP不重复随机数】

<% 
function rndarray(istart,iend,sum) 
dim arrayid(),i,j,blnre,temp,iloop,eloop 
redim arrayid(sum-1) 
i=0 
iloop=0 
eloop=0 
blnre=false 
randomize 
do while i<sum 
temp=int(rnd*(iend-istart+1)+istart) 
if i=0 then 
arrayid(0)=temp 
i=i+1 
iloop=iloop+1 
else 
for j=0 to i-1 
if arrayid(j)=temp then 
blnre=true 
iloop=iloop+1 
exit for'这一句很重要,防止多余的循环 
else 
iloop=iloop+1 
end if 
next 
 if blnre=false then 
 arrayid(i)=temp 
 i=i+1 
 else 
 blnre=false 
 end if 
end if 
loop 
rndarray=join(arrayid) 
end function 
response.write rndarray(1,9,6) '开始数字,结尾数字,生成多少个 

response.End()'终止程序
%>

结果:9 2 1 3 8 5

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

橙-极纪元JJY.Cheng

客官,1分钱也是爱,给个赏钱吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值