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
LVL 1
Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

IT / Software Engineering ConsultantCommented:
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
0
Commented:
Assuming it can be anything in the range 0000-9999...

TheCode = Right("000" & Int(Rnd * 10000), 4)
0
Author Commented:
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 ?
0
IT / Software Engineering ConsultantCommented:
Okay, in this case I think you want something like this, but I welcome other experts insight.  First, create a function to generate the 4 digit number:

<% function randCode
dim max,min
max=9999
min=1000
randomize
randCode=(Int((max-min+1)*Rnd+min))
end function
%>

you can reference it like this:

<%=randCode%>

~bp
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Great ! .. thanks a million. I finished my page now .. :)
0
IT / Software Engineering ConsultantCommented:
Excellent, glad that helped, thanks for the points.

~bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.