473,471 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Randomly Generated Strings

Hi All,

I used to have a piece of code that would generate a random string of
8,16,24 or 32 characters in length.

I got it from this group, does any one have this piece of code?

It will be used for a double opt-in email newsletter.
Many thanks,

Stuart
Jul 19 '05 #1
3 2220
It should be something like (out of my head, not tested)

function GetRandomString(len as integer)
Dim strLetters as string
Dim strResult as String
Dim i as Integer
strLetters = '01234567890ABCDEFHIJKLMNOPQRSTUVWZYZ'

For i = 0 to len
strResult = strResult & SUBSTR(strLetters , FLOOR(RAND() * 36))+1, 1)
Next
GetRandomString = strResult
End function

--
Roji. P. Thomas
SQL Server Programmer
"Stuart" <st****@nowhere.co.uk> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...
Hi All,

I used to have a piece of code that would generate a random string of
8,16,24 or 32 characters in length.

I got it from this group, does any one have this piece of code?

It will be used for a double opt-in email newsletter.
Many thanks,

Stuart

Jul 19 '05 #2
"Roji. P. Thomas" <la********@nowhere.com> wrote in message
news:O3**************@TK2MSFTNGP12.phx.gbl...
It should be something like (out of my head, not tested)

function GetRandomString(len as integer)
Dim strLetters as string
Dim strResult as String
Dim i as Integer
strLetters = '01234567890ABCDEFHIJKLMNOPQRSTUVWZYZ'

For i = 0 to len
strResult = strResult & SUBSTR(strLetters , FLOOR(RAND() * 36))+1, 1)
Next
GetRandomString = strResult
End function


Close, but you need the Randomize statement first. Also, "len" is a
keyword. Try this:

Function GetRandomString( numChars )
Randomize
strLetters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklm nopqrstuvwxyz"
iLettersLen = Len(strLetters)
strResult = ""

For i = 1 To numChars
x = CInt((iLettersLen * Rnd) + 1)
strResult = strResult & Mid(strLetters,x,1)
next

GetRandomString = strResult
End Function

Regards,
Peter Foti
Jul 19 '05 #3
Many thanks Guys.
I will save the code away somewhere safe - but not so safe that I cannot
find it.

;o))

"Peter Foti" <pe***@Idontwantnostinkingemailfromyou.com> wrote in message
news:10*************@corp.supernews.com...
"Roji. P. Thomas" <la********@nowhere.com> wrote in message
news:O3**************@TK2MSFTNGP12.phx.gbl...
It should be something like (out of my head, not tested)

function GetRandomString(len as integer)
Dim strLetters as string
Dim strResult as String
Dim i as Integer
strLetters = '01234567890ABCDEFHIJKLMNOPQRSTUVWZYZ'

For i = 0 to len
strResult = strResult & SUBSTR(strLetters , FLOOR(RAND() * 36))+1, 1)
Next
GetRandomString = strResult
End function


Close, but you need the Randomize statement first. Also, "len" is a
keyword. Try this:

Function GetRandomString( numChars )
Randomize
strLetters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklm nopqrstuvwxyz"
iLettersLen = Len(strLetters)
strResult = ""

For i = 1 To numChars
x = CInt((iLettersLen * Rnd) + 1)
strResult = strResult & Mid(strLetters,x,1)
next

GetRandomString = strResult
End Function

Regards,
Peter Foti

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: MattO | last post by:
Here is the background information: *** *** *** *** *** *** *** *** In order to display enum string values in my application with localized values, I created a class called MyEnumConverter that...
5
by: Raghuram | last post by:
I want to generate some set of chars which are more than 4 chars randomly. . .. how can i do that
2
by: Scott Gordo | last post by:
<!--Newbie warning--> I've got a csv file with names, addresses, emails, etc. I've been asked to randomly select a name from a csv file. I've found plenty of RandNum examples, but I'm not sure how...
1
by: Suresh369 | last post by:
HI, I have a need to randomly access a Map element. For this I tried: int entry ; //Randomly generated map<int, int> blah; map<int, int>::iterator blahI; blahI = element + blah.begin();
3
by: vadapallyraju | last post by:
Hi Friends, I am relatively new to this generation of variables in ASP. I am unable to proceed from here. Can somebody advise ? Challenge : I get number of participants in an exam, from a sql...
3
Nert
by: Nert | last post by:
hi everyone, Could anyone please teach me how to make the randomly generated text as an image? same as the image we have seen when we were signing up in this forum (www.thescripts.com). this is my...
4
by: stevong | last post by:
Hi everyone, I have a mailing list generated from php and sent out by mail(). But i often have broken graphics due to the fact that it randomly appends %20 to the html output! Anybody has...
2
by: tamaker | last post by:
Is this do-able with ASP / VBscript? -- I have a database with user records (name, photo, etc. etc.) I want to use asp to generate (on the homepage) a series of 4 randomly selected 'user...
1
by: sugard | last post by:
Hey, I need to generate an id number randomly. This number generated must be unique. Also though this number must be saved as a String. Can please some1 help me and tell me how to do this? ...
4
by: vishal1082 | last post by:
i m creating a app that can make backup of ur browser data, i m stucked for firefox, %APPDATA%\Mozilla\Firefox\Profiles\ - this is the address for all of your bookmarks and everything in...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.