
实际一般需要从数据库中随机提取N道题目。
以下代码都基于VBS;
通常的编写类似这样的
| "产生不重复随机数 for j=0 to i-1 if blnre=false then end if response.write rndarray(1,10,5)&"<br>""调用过程 |
PS。其中的iloop、eloop是为了计算循环次数而已。
以上,大多数人的都是用这种方法编写的,生成一个随机数,然后再和以前生成的做比较,判断是否可用;
但这不是一种AI的,或者说有效率的方法,为什么不用两个数组呢?
数组1,存放需要的字符串,或数字等,数组2存放生成的随机数;当每次随机生成中间变量temp的一个下标x,赋给数组2,然后从数组1中,去掉下标为x的数字,赋给中间变量temp;这样每生成一个随机数,就从数组1中拿掉这个数,下次再生成一个数就不会重复了,这种产生随机数的方法原理实际是从数组1中提取。
方法二
| function rndstr(istart,iend,isum) dim vntarray2(),temp,x,y response.write rndstr(1,5,2) |
这样,是不是更简单呢
展开一下,假如要生成随机字符串,包含字母数字时,仅需为数组1赋值时,用函数chr(num);
假设,需要做一个手机中奖的页面程序。
首先把值赋给数组1,可以130....~139....循环赋值,当然实际使用时从数据库中把已有的赋值,然后再随机提取赋给数组2;
最后,再修饰一下
| temp=replace(join(array2),chr(32),"") phone=left(temp,6)&"***"&right(temp,2) |
得到类似137648***58的结果,呵呵
写那么多,累死了~~
Disclaimer: The content on this page is collected and edited by Jinghuang Network for reference only. We do not claim ownership or bear legal responsibility for external materials. If you find any copyright infringement, please contact us with proof, and we will remove the content within 5 working days. For more insights on Global Web Optimization and Global Web Development, please visit our official site (www.0731jianzhan.com).
24/7 Support
Global hotline anytime
50x Compensation
For service downtime
7-Day Refunds
On eligible products
Pro Teams
Global branch service
10+ Yrs History
Global Branches
Senior Devs
10K+ Clients
Partner
Grow and win together
Build marketing ecosystems
Connect
Seamless user bridges
Future
Shared platform dev
0731-82272030
Address: Room 1202, North Bldg, Xingwei Mingzuo, Yuhua Dist, Changsha
Jinghuang Network specializes in corporate sites, e-commerce, apps, and full-stack systems.
We provide stable, high-performance platforms with full tech support and maintenance.
WeChat Official