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
AleksAsked:
Who is Participating?
 
Bill PrewCommented:
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
 
Bill PrewCommented:
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
 
Patrick MatthewsCommented:
Assuming it can be anything in the range 0000-9999...


TheCode = Right("000" & Int(Rnd * 10000), 4)
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
AleksAuthor 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
 
AleksAuthor Commented:
Great ! .. thanks a million. I finished my page now .. :)
0
 
Bill PrewCommented:
Excellent, glad that helped, thanks for the points.

~bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.