Click to See Complete Forum and Search --> : Random Numbers & Letters


weee
02-29-2004, 04:26 PM
Hi There.

How can I generate 8 characters made of numbers and letters to be choosen in a random way and show it to the user later.

Thanks -
Wee

PeOfEo
02-29-2004, 04:29 PM
rnd is the keyword

Public Function RandomString(ByVal LowerBound As Integer, ByVal UpperBound As Integer, ByVal StringLength As Integer) As String

Dim i As Integer

Rnd (1)
Randomize

For i = 1 To StringLength
RandomString = RandomString & Chr(Int((UpperBound - LowerBound + 1) * Rnd + LowerBound))
Next i

End Function

just something I pulled off of a site I found on google... this is in regular vb thought. Should be about the same in vbscript and it will work in vb.net.

weee
02-29-2004, 04:43 PM
I'm not sure how to use it...

PeOfEo
02-29-2004, 04:46 PM
Dim i As Integer 'declares i

Rnd (1) 'it is random between 0 and 1
Randomize 'pick the random number
The other part is a loop that creates the random string from i. You will have to forgive me though, I have sort of forgotten the rnd syntax since it has been so long since I have used it. I have it in the appendex of a book of mine, unfortunatly my friend is borrowing it right now.

buntine
02-29-2004, 10:29 PM
This example wont work in regular ASP.
If you are using classic ASP then post again and i (or someone else) will help you out.

PeOfEo
02-29-2004, 10:30 PM
Oh, that rnd syntax does not work in classic? Shows how much I know about classic, I know this is a vb6 example but I was better it would still run in vbscript.

buntine
02-29-2004, 10:37 PM
The rnd will work, but alot of the other snytax wont be valid.

In vbScript, you dont need to define a variables data type when you dimension it.
Randomize - Is not an ASP keyword either.. Though, its helpful when using VB.NET

That function is a bit weird anyway.. Functions are supposed to return a value.. In C, C++ you would get an error. The author should have used a sub-routine.

Regards.

PeOfEo
02-29-2004, 10:52 PM
I agree about the sub. A function does not have to return in c++ though does it? In java (yes I know java sadly enough) if you do not specify a return data type then there is no return. Mainly its the set functions that do that, or a main function, or a constructor, or just some place where I am printing a bunch of crap on the screen :p

buntine
02-29-2004, 11:01 PM
No in C++ it should be ok.. But C and Java definetely return an error.

PeOfEo
02-29-2004, 11:05 PM
Originally posted by buntine
No in C++ it should be ok.. But C and Java definetely return an error. Well if you lop off that return type it would run fine in java.

public void printcrap(String crap)
{
System.out.println(crap);
}

This is a java function, no return, no return type. I am just passing in a string and printing it.

buntine
02-29-2004, 11:11 PM
Yes, but you've using the 'void' keyword to tell the bytecode compiler that this function wont return a value..