Important alert: (current site time 7/16/2013 5:46:42 AM EDT)
 

VB icon

A Random String Generator With Patterns and Extra Options

Email
Submitted on: 5/4/2003 4:47:18 PM
By: VoodooSnake 
Level: Beginner
User Rating: By 1 Users
Compatibility: Delphi 7, Delphi 6, Delphi 5, Delphi 4
Views: 12569
 
     Creates Random Strings of a specific length. There r sum options that u can use, and u can set ur own pattern. So, it can b pretty useful. Hope it is. :) if u want to c the code implemented in a Program, u can take a look at my code example that i submitted. I'll put the link when i get it. (Just copy-and-paste the code to delphi, it might help with the formatting. Sry about that, havnt dominated code uploading yet :)) thx
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
!**************************************
! Name: A Random String Generator With Patterns and Extra Options
! Description:Creates Random Strings of a specific length. There r sum options that u can use, and u can set ur own pattern. So, it can b pretty useful. Hope it is. :) if u want to c the code implemented in a Program, u can take a look at my code example that i submitted. I'll put the link when i get it. (Just copy-and-paste the code to delphi, it might help with the formatting. Sry about that, havnt dominated code uploading yet :)) thx
! By: VoodooSnake
!
!This code is copyrighted and has! limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1115&lngWId=7!for details.!**************************************

Function GenStr(MaxLen,Restrictions:Integer):String;Overload;
{Possible Resctiction Values:
1 - All
2 - Just Letters and Numbers
3 - Just Numbers
4 - Just Letters
}
Var St:Byte;
Total:String;
begin
Repeat
Randomize;
St := Random(255);
// All Caracters
If Restrictions = 1 then
 Total := Total + chr(St);
//Just Letters and Numbers
If Restrictions = 2 then
 If (St >= 48) and (St <= 57) or (St >= 65) and (St <= 90) or (St >= 97) and (St <= 122) then
Total := Total + chr(St);
//Just Numbers
If Restrictions = 3 then
 If (St >= 48) and (St <= 57) then
Total := Total + chr(St);
//Just Letters
If Restrictions = 4 then
 If (St >= 65) and (St <= 90) or (St >= 97) and (St <= 122) then
Total := Total + chr(St);
Until Length(Total) = MaxLen;
Result := Total;
end;
Function GenStr(MaxLen:Integer;Pattern:String):String;Overload;
{Possible Resctiction Values:
1 - All
2 - Just Letters and Numbers
3 - Just Numbers
4 - Just Letters
 Possble Pattern Parameters:
A or a - All
L - Letter In Capitals
l - Letter not in capitals
C or c - Character ( A,B or a,b (etc.))
N or n - Numbers
R or r - Random Generated Pattern
 Ex:
If u wanted a generated string that was Number, number,number,Captiol
letter then any letter, u would use this pattern:
NNNLC or NNNLc
}
Var St:Byte;
Total:String;
Ptrn:String;
P:Byte;
PtrnCount:Integer;
begin
//If pattern is set to random, we need to make a pattern ourselves
If (Pattern = 'R') or (Pattern = 'r') then
Begin
repeat
 //Get ready for random
 Randomize;
 //Get random number
 P := Random(225);
 //Check to c if its a pattern number or not (ASCII values of A,N,L,l,etc.
 Case P of
//A or a
65 or 97:Ptrn := Ptrn + 'A';
//L
78:Ptrn := Ptrn + 'L';
//l
108:Ptrn := Ptrn + 'l';
//C or c
67 or 99:Ptrn := Ptrn + 'C';
//N or n
78 or 110:Ptrn := Ptrn + 'N';
 end;
until Length(Ptrn) = (MaxLen div 2) + 1;
//Set the New Pattern
Pattern := Ptrn;
 end;
//String Generation Section
PtrnCount := 0;
Repeat
//Prepare to random
Randomize;
//Add 1 to PtrnCount
Inc(PtrnCount);
 //A or a //Everything Is OK
 If (Pattern[PtrnCount] = 'A') or (Pattern[PtrnCount] = 'a') then
Total := Total + chr(Random(255));
 //L //Just Capitol Letters
 If (Pattern[PtrnCount] = 'L') then
Begin
Repeat
 //Get random number
 St := Random(255);
Until (St >= 65) and (St <= 90);
Total := Total + chr(St);
end;
 //l //Letters, but not it capitols
 If (Pattern[PtrnCount] = 'l') then
Begin
Repeat
 //Get random number
 St := Random(255);
Until (St >= 97) and (St <= 122);
Total := Total + chr(St);
end;
 //C or c //Any letter, wether its a capitol or not
 If (Pattern[PtrnCount] = 'C') or (Pattern[PtrnCount] = 'c') then
begin
 Repeat
//Get random number
St := Random(255);
 Until (St >= 65) and (St <= 90) or (St >= 97) and (St <= 122);
 Total := Total + chr(St);
end;
 //N or n //Number
 If (Pattern[PtrnCount] = 'N') or (Pattern[PtrnCount] = 'n') then
begin
 Repeat
//Get random number
St := Random(255);
 Until (St >= 48) and (St <= 57);
 Total := Total + chr(St);
end;
//IF PtrnCount is at it's max, reset it
If PtrnCount = Length(Pattern) then
 PtrnCount := 0;
Until Length(Total) = MaxLen;
Result := Total;
end;


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.