ASP Function not returning what I want

D

Drew

I am trying to build a page to generate some random data for a database that
I am working on... I thought this would be simple, but it has proven to be
more difficult... I am trying to create some functions to build a random
date. It keeps returning a string like, "9172006//" instead of "9/17/2006".
What's making this do that?

Function RandomNumber(min,max)
dim rand
rand = Int((max-min+1)*Rnd+min)
Response.Write(rand)
End Function

Function RandomDate()
dim M
dim D
dim Y
dim randdate
M = RandomNumber(1,12)
If M <> 2 OR M <> 4 OR M <> 6 OR M <> 9 OR M <> 11 Then
D = RandomNumber(1,31)
ElseIf M <> 2 Then
D = RandomNumber(1,30)
ElseIf M = 2 Then
D = RandomNumber(1,28)
End If
Y = RandomNumber(2005,2007)
randdate = M & "/" & D & "/" & Y
Response.Write(randdate)
End Function

For i = 1 to 500
varRandomDate = RandomDate()
End If

Thanks!
Drew
 
A

Anthony Jones

Drew said:
I am trying to build a page to generate some random data for a database that
I am working on... I thought this would be simple, but it has proven to be
more difficult... I am trying to create some functions to build a random
date. It keeps returning a string like, "9172006//" instead of "9/17/2006".
What's making this do that?

Function RandomNumber(min,max)
dim rand
rand = Int((max-min+1)*Rnd+min)
Response.Write(rand)
End Function

In order for a function to return a value you must assign the result to the
name of the function hence the above becomes:-

Function RandomNumber(min,max)
RandomNumber= Int((max-min+1)*Rnd+min)
End Function

Function RandomDate()
dim M
dim D
dim Y
dim randdate
M = RandomNumber(1,12)
If M <> 2 OR M <> 4 OR M <> 6 OR M <> 9 OR M <> 11 Then
D = RandomNumber(1,31)
ElseIf M <> 2 Then
D = RandomNumber(1,30)
ElseIf M = 2 Then
D = RandomNumber(1,28)
End If
Y = RandomNumber(2005,2007)

randdate = M & "/" & D & "/" & Y
Response.Write(randdate)

Again these two lines become:-

RandomDate = M & "/" & D & "/" & Y

End Function
For i = 1 to 500
varRandomDate = RandomDate()
End If

To see the results you need:-

For i = 1 to 500
Response.Write RandomDate() & "<br />"
Next
 
B

Bob Barrows [MVP]

Drew said:
I am trying to build a page to generate some random data for a
database that I am working on... I thought this would be simple, but
it has proven to be more difficult... I am trying to create some
functions to build a random date. It keeps returning a string like,
"9172006//" instead of "9/17/2006". What's making this do that?
Your RandomNumber function isn't returning anything.
Function RandomNumber(min,max)
dim rand
rand = Int((max-min+1)*Rnd+min)
Response.Write(rand) RandomNumber = rand
End Function

Neither is the RandomDate function. Same problem
Writing to Response does not cause the value to be returned to the calling
procedure.
 
J

Justin Piper

Again these two lines become:-

RandomDate = M & "/" & D & "/" & Y

Also, a much easier way to generate a random date is to use DateAdd. A
function that produces a random date in 2005, 2006 or 2007 would look
like this:

Function RandomDate
Const StartDate = #2005-01-01#
Const EndDate = #2007-12-31#

Dim period: period = DateDiff("d", StartDate, EndDate) + 1
RandomDate = DateAdd("d", Int(Rnd * period), StartDate)
End Function

And a simple change would allow you to supply the start and end date as
arguments:

Function RandomDate(startdate, enddate)
Dim period: period = DateDiff("d", startdate, enddate) + 1
RandomDate = DateAdd("d", Int(Rnd * period), startdate)
End Function

You can find the documentation for DateAdd on Microsoft's website.

DateAdd Function
http://msdn2.microsoft.com/en-us/library/cb7z8yf9.aspx
 
E

Evertjan.

Bob Barrows [MVP] wrote on 18 jul 2007 in
microsoft.public.inetserver.asp.general:
Your RandomNumber function isn't returning anything.


Neither is the RandomDate function. Same problem
Writing to Response does not cause the value to be returned to the
calling procedure.

Randomize

Initializes the random-number generator.

Randomize uses number to initialize the Rnd function's random-number
generator, giving it a new seed value. If you omit number, the value
returned by the system timer is used as the new seed value.

If Randomize is not used, the Rnd function (with no arguments) uses the
same number as a seed the first time it is called, and thereafter uses
the last generated number as a seed value.
 
D

Drew

Thanks for the help, that function is a lot better!

Thanks,
Drew

Again these two lines become:-

RandomDate = M & "/" & D & "/" & Y

Also, a much easier way to generate a random date is to use DateAdd. A
function that produces a random date in 2005, 2006 or 2007 would look
like this:

Function RandomDate
Const StartDate = #2005-01-01#
Const EndDate = #2007-12-31#

Dim period: period = DateDiff("d", StartDate, EndDate) + 1
RandomDate = DateAdd("d", Int(Rnd * period), StartDate)
End Function

And a simple change would allow you to supply the start and end date as
arguments:

Function RandomDate(startdate, enddate)
Dim period: period = DateDiff("d", startdate, enddate) + 1
RandomDate = DateAdd("d", Int(Rnd * period), startdate)
End Function

You can find the documentation for DateAdd on Microsoft's website.

DateAdd Function
http://msdn2.microsoft.com/en-us/library/cb7z8yf9.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,093
Messages
2,570,607
Members
47,227
Latest member
bluerose1

Latest Threads

Top