Link to home
Start Free TrialLog in
Avatar of Aleks
AleksFlag for United States of America

asked on

Vbscript to generate random 4 digit code

I have an ASP/VBscript page. I need to assing a randome 4 digit passcode to a hidden field element. I need a function for that is my guess. Can someone help with this ? and what is the value i should give to the hidden element.

Thanks !
A
Avatar of Bill Prew
Bill Prew

You will need to use the RND function, which returns a 0 to 1 value (decimals).  So you will need to do some math with that to normalize it into the range of values you want.  Take a look at the code at this link and let us know if you still have questions.

http://www.w3schools.com/vbscript/func_rnd.asp

~bp
Assuming it can be anything in the range 0000-9999...


TheCode = Right("000" & Int(Rnd * 10000), 4)
Avatar of Aleks

ASKER

I found this one;

<script type="text/vbscript">

Dim max,min
max=100
min=1
Randomize
document.write(Int((max-min+1)*Rnd+min))

</script>

If I need 4 numbers I could use:

<script type="text/vbscript">

Dim max,min
max=9999
min=1000
Randomize
document.write(Int((max-min+1)*Rnd+min))

</script>

That way it needs to have 4 digits, now .. how do I call this function to set the value of the hidden field to the results of the above ?
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aleks

ASKER

Great ! .. thanks a million. I finished my page now .. :)
Excellent, glad that helped, thanks for the points.

~bp