ASP Function not returning what I want
ASP Function not returning what I want
am 17.07.2007 22:57:29 von 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
Re: ASP Function not returning what I want
am 17.07.2007 23:32:16 von Anthony Jones
"Drew" wrote in message
news:ONRJYULyHHA.4276@TK2MSFTNGP05.phx.gbl...
> 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() & "
"
Next
--
Anthony Jones - MVP ASP/ASP.NET
Re: ASP Function not returning what I want
am 18.07.2007 00:22:28 von reb01501
Drew wrote:
> 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.
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Re: ASP Function not returning what I want
am 18.07.2007 00:53:58 von Justin Piper
On Tue, 17 Jul 2007 16:32:16 -0500, Anthony Jones =
=
wrote:
> "Drew" wrote in message
> news:ONRJYULyHHA.4276@TK2MSFTNGP05.phx.gbl...
>> 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 RandomDate()
>> dim M
>> dim D
>> dim Y
>> dim randdate
>> M =3D RandomNumber(1,12)
>> If M <> 2 OR M <> 4 OR M <> 6 OR M <> 9 OR M <> 11 Then
>> D =3D RandomNumber(1,31)
>> ElseIf M <> 2 Then
>> D =3D RandomNumber(1,30)
>> ElseIf M =3D 2 Then
>> D =3D RandomNumber(1,28)
>> End If
>> Y =3D RandomNumber(2005,2007)
>
>
>> randdate =3D M & "/" & D & "/" & Y
>> Response.Write(randdate)
>
> Again these two lines become:-
>
> RandomDate =3D M & "/" & D & "/" & Y
>
>> End Function
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 =3D #2005-01-01#
Const EndDate =3D #2007-12-31#
Dim period: period =3D DateDiff("d", StartDate, EndDate) + 1
RandomDate =3D 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 =3D DateDiff("d", startdate, enddate) + 1
RandomDate =3D 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
-- =
Justin Piper
Bizco Technologies
http://www.bizco.com/
Re: ASP Function not returning what I want
am 18.07.2007 09:08:03 von exjxw.hannivoort
Bob Barrows [MVP] wrote on 18 jul 2007 in
microsoft.public.inetserver.asp.general:
> Drew wrote:
>> 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.
>
>
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.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Re: ASP Function not returning what I want
am 18.07.2007 18:05:11 von Drew
Thanks for the help, that function is a lot better!
Thanks,
Drew
"Justin Piper" wrote in message
news:op.tvmsj8rvcs3d1w@bizco-ps-jpiper.bizdom01.bizcotech.co m...
On Tue, 17 Jul 2007 16:32:16 -0500, Anthony Jones
wrote:
> "Drew" wrote in message
> news:ONRJYULyHHA.4276@TK2MSFTNGP05.phx.gbl...
>> 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 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
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
--
Justin Piper
Bizco Technologies
http://www.bizco.com/