VBScript中产生相同的随机数时循环 - 如何解决? [英] VBScript generating same random number when in loop - how to solve?

查看:105
本文介绍了VBScript中产生相同的随机数时循环 - 如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个功能,8个字符长产生数字的随机字符串。它的工作原理如果每页调用一次,也就是说,如果我刷新,它会显示一个新号码。

So I have this function which generates a random string of digits 8 characters long. It works if its called once per page, ie if I refresh it will show a new number.

不过,我想生成许多这样的一个循环,并返回其相同数量的内部。我怎样才能解决这个问题?

But I want to generate many of these inside a loop and its returning the same number. How can I solve this?

Function generateCode()

   pChar = "0123456789"

   pCount = Len(pChar) 

   Dim psw
   psw = ""

   Randomize
   For i = 1 To 8 ' password length
      psw = psw & Mid( pChar, 1 + Int(Rnd * pCount), 1 )
   Next

 generateCode= psw
End Function

现在我想随机化,可以根据断当前的时间,所以我采取了随机化线路输出和调用生成code(循环之前调用随机化)I-仍然没有工作!

Now I thought Randomize may be based off the current time, so I took the Randomize line out and called Randomize before the loop that calls generateCode() i- still didn't work!

推荐答案

<一个href=\"http://ns7.webmasters.com/caspdoc/html/vbscript_randomize_statement.htm\"><$c$c>Randomize不带任何参数种子伪随机数发生器使用的系统时间。如果你非常迅速调用它多次系统时间不会改变,所以你每次都会重新初始化PRNG使用相同的种子,在相同随机数。

Randomize without any arguments seeds the pseudo-random number generator using the system time. If you call it multiple times very quickly the system time won't have changed so you will reinitialize the PRNG with the same seed each time, giving the same random numbers.

您应该只是你的网页,而不是多次调用上随机化只有一次。

You should only call Randomize only once on your page, not multiple times.

这篇关于VBScript中产生相同的随机数时循环 - 如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆