Advanced Random Module - BerryP - Nov 21, 2014
Hey, I am quickly sharing this source with you. Its a module with witch you can generate random chars and strings. But it has some advandced options.
I needed it for a program and thought to share it with the community :)
Here are all the options it got:- Letters,Numbers
- Letters
- Letters small
- Letters big
- Numbers
- Letters small, numbers
- Letters big,Numbers
- Letters,Numbers,Special
- Numbers,Special
- Everything
Module
PHP Code
<?php Module RandomGenerator
''' <summary> ''' Module created my Maember @ HF. Please, leave credits if you share the code. ''' </summary> ''' <remarks></remarks>
Private Random As New Random(Environment.TickCount)
Friend Enum RandomStringType As Byte Alphanumeric = 0 Letters = 1 Lowercase = 2 Uppercase = 3 Numeric = 4 UppercaseNumeric = 5 LowercaseNumeric = 6 AlphanumericSpecial = 7 NumericSpecial = 8 Everything = 9 End Enum
Friend Function RandomString(ByVal fromL As Integer, ByVal toL As Integer) As String Return GenerateRandom(Random.Next(fromL, toL + 1), RandomStringType.Letters) End Function
Friend Function GenerateRandom(ByVal length As Integer, ByVal type As RandomStringType) As String Dim r As String = "" For i As Integer = 0 To length - 1 Select Case type Case RandomStringType.Everything r &= RandomEverything() Case RandomStringType.Letters r &= RandomLetter() Case RandomStringType.Uppercase r &= RandomUpperCase() Case RandomStringType.Lowercase r &= RandomLowerCase() Case RandomStringType.UppercaseNumeric Select Case RandomNumber(0, 1) Case 0 r &= RandomUpperCase() Case 1 r &= RandomNumber() End Select Case RandomStringType.LowercaseNumeric Select Case RandomNumber(0, 1) Case 0 r &= RandomLowerCase() Case 1 r &= RandomNumber() End Select Case RandomStringType.Alphanumeric Select Case RandomNumber(0, 1) Case 0 r &= RandomLetter() Case 1 r &= RandomNumber() End Select Case RandomStringType.AlphanumericSpecial Select Case RandomNumber(0, 2) Case 0 r &= RandomLetter() Case 1 r &= RandomNumber() Case 2 r &= RandomSpecial() End Select Case RandomStringType.Numeric r &= RandomNumber() Case RandomStringType.NumericSpecial Select Case RandomNumber(0, 1) Case 0 r &= RandomSpecial() Case 1 r &= RandomNumber() End Select End Select Next Return r End Function
Friend Function GenerateRandom(ByVal fromL As Integer, ByVal toL As Integer, ByVal type As RandomStringType) As String Return GenerateRandom(RandomNumber(fromL, toL), type) End Function
Friend Function RandomNumber() As Char Return ChrW(Random.Next(48, 58)) End Function
Friend Function RandomNumber(ByVal fromL As Integer, ByVal toL As Integer) As Integer Return Random.Next(fromL, toL + 1) End Function
Friend Function RandomUpperCase() As Char Return Chr(Random.Next(65, 91)) End Function
Friend Function RandomLowerCase() As Char Return Chr(Random.Next(97, 123)) End Function
Friend Function RandomLetter() As Char Select Case RandomNumber(0, 1) Case 1 Return RandomUpperCase() Case 0 Return RandomLowerCase() End Select Return Nothing End Function
Friend Function RandomSpecial() As Char Return Chr(Random.Next(33, 64)) End Function
Friend Function RandomLetterNumericSpecial() As Char Select Case RandomNumber(0, 2) Case 0 Return RandomLetter() Case 1 Return RandomNumber() Case 2 Return RandomSpecial() End Select Return Nothing End Function
Friend Function RandomEverything() As Char Select Case RandomNumber(0, 9) Case 0 Return RandomLetter() Case 1 Return RandomNumber() Case 2 Return RandomSpecial() Case 3 Return RandomChar(123, 128) Case 4 Return RandomChar(130, 140) Case 5 Return Chr(142) Case 6 Return RandomChar(145, 156) Case 7 Return RandomChar(158, 159) Case 8 Return RandomChar(161, 172) Case 9 Return RandomChar(174, 255) End Select Return Nothing End Function
Friend Function RandomChar(ByVal fromL As Integer, ByVal toL As Integer) As Char Return Chr(RandomNumber(fromL, toL)) End Function
End Module
This is how you use it:
Code RandomGenerator.GenerateRandom(10, 15, RandomStringType.Everything)
Will generate things like that:
«ÇŸ0d*©5.2
¦Žª-Ÿ‡©Ž‡›
«4™€ŠœŽ}Ž}|„ö
Hope you like it 🎉👍
If you find bugs dont hestiate to write them in the thread 🤝
Peace ✌
* - Nivarka - Nov 21, 2014
Thanks!
I need something like this for a project I'm working on.
As part of a login reset I'm having the system email the user with a random generated string, and storing that string in the database. If the user enters the string from their email, they can reset the password.
This will help a lot with that, much more secure than the standard way of doing things.
* - BerryP - Nov 21, 2014
Hey, always love to hear good reviews from HQ members. 🤗
Cool that it helped you.
Peace 🤝
* - taktaktak - Nov 21, 2014
Thanks for the post, may actually come in handy!
* - Hiiro - Nov 21, 2014
You should rename the enums to their technical terms like for example:
LettersNumbers
>
Alphanumeric
I think that would be cool,
thanks for the contribution I think most people would find this useful. :P
* - IAmNotHere - Nov 21, 2014
Good written source, without errors and doing as supposed. Thank you.
Edit:
Quote:Letters,Numbers
Letters
Letters small
Letters big
Numbers
Letters small, numbers
Letters big,Numbers
Letters,Numbers,Special
Numbers,Special
Everything
Just a tip.
Where "Letters" you can let the the source code user know that you mean Letter "UPPERCASE" and Letter "lowercase" combined.
For the rest I have a cool string generator now.
Regards.
* - He B TeMy - Nov 22, 2014
(Nov 21, 2014 07:27 PM)IAmNotHere Wrote: Good written source, without errors and doing as supposed. Thank you.
Edit:
Just a tip.
Where "Letters" you can let the the source code user know that you mean Letter "UPPERCASE" and Letter "lowercase" combined.
For the rest I have a cool string generator now.
Regards.
"Good written source" coming from a guy who made https://hackforums.net/showthread.php?tid=4539753
* - IAmNotHere - Nov 22, 2014
Probably I can't beat you, but I have a c++ crypter.
* - BerryP - Nov 22, 2014
(Nov 21, 2014 05:55 PM)Austrix Wrote: You should rename the enums to their technical terms like for example:
LettersNumbers
>
Alphanumeric
I think that would be cool,
thanks for the contribution I think most people would find this useful. :P
Going to do that, I am not an native speaker and didnt know the real terms 🤐 😊
Thank you, for the suggsestion and the positive feedback :)
@Glory2Hypno Thanks for the positive feedback ✌
@IAmNotHere Thanking you too :) Yes, I am going to change that 🤝
Peace to all
Peace 🤝
Edit: Changed the enum names. 🤝
|