How to generate a number of unique/random strings?
-
- Always Here
- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
How to generate a number of unique/random strings?
Can we do this with PB? :http://www.den4b.com/?x=tools&tool=password
I'm looking for a way to produce Keys and Initialisation Vectors for AESEncoder()
I'm looking for a way to produce Keys and Initialisation Vectors for AESEncoder()
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
-
- Addict
- Posts: 4775
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: How to generate a number of unique/random strings?
Hi,
this is a basic version, not too slow.
Of course it can be adapted, so that there will be different probabilities for different characters and whatnot.
this is a basic version, not too slow.
Of course it can be adapted, so that there will be different probabilities for different characters and whatnot.

Code: Select all
EnableExplicit
Procedure.s RandomString (numChars.i, allowedCharacters$="ABCabc123")
; -- works in ASCII mode and Unicode mode
Protected *sourceBase, *source.Character, *dest.Character
Protected lastAllowed.i=Len(allowedCharacters$)-1
Protected ret$ = Space(numChars)
*sourceBase = @allowedCharacters$
*dest = @ret$
While *dest\c <> 0
*source = *sourceBase + Random(lastAllowed) * SizeOf(Character)
*dest\c = *source\c
*dest + SizeOf(Character)
Wend
ProcedureReturn ret$
EndProcedure
Debug RandomString(10)
Last edited by Little John on Thu Jun 18, 2015 5:19 am, edited 1 time in total.
Re: How to generate a number of unique/random strings?
This does the easy part:
This shows one way to eliminate a stretch of characters.
Code: Select all
For i = 1 To 10
s$ + Chr(Random(126,32))
Next
Debug s$
Code: Select all
i = 0
Repeat
t$ = Chr(Random(126,32))
a = Asc(t$)
If a <90 Or a > 96
s$ + t$
i+1
EndIf
Until i = 10
Debug s$
- Zebuddi123
- Enthusiast
- Posts: 796
- Joined: Wed Feb 01, 2012 3:30 pm
- Location: Nottinghamshire UK
- Contact:
Re: How to generate a number of unique/random strings?
Hi IdeasVacuum Just show`s Little John`s version using pointer`s is twice as fast. 100,000 iterations average 450ms, mine 850ms. on my i3 lappy.
Zebuddi.
Zebuddi.

Code: Select all
Procedure.s GenData(maxlen)
Protected rn, string.s, number
RandomSeed(Random(100000, 1))
While number<maxlen
rn =Random(122, 48)
Select rn
Case 48 To 57, 65 To 90, 97 To 122
string+Chr(rn)
number+1
EndSelect
Wend
ProcedureReturn string
EndProcedure
For i=1 To 10
Debug GenData(10)
Next
malleo, caput, bang. Ego, comprehendunt in tempore
-
- Always Here
- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: How to generate a number of unique/random strings?
whoosh! thanks guys 

IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
Re: How to generate a number of unique/random strings?
I've been using this in my password manager for years. It's not fast but if you're just generating passwords speed is not an issue. I took care to print only character that I could actually read with my old eyes.. I can't tell the difference between ! and I without a magnifying glass LOL!
Good Luck!
Jack
Code: Select all
Procedure.s KeyGen(KeyLen)
Define OldNum = Random(3)
Define Use, x
Define LongKey$, PassKey$
For X = 1 To 1000
Repeat
Use = Random(3) ; numeric 0-3
Until Use <> OldNum
OldNum = Use
Select Use
Case 0
LongKey$ + Str(Random(9)) ; numbers 0 to 9
Case 1
LongKey$ + Chr(Random(25) + 65) ; uppercase A to Z
Case 2
LongKey$ + Chr(Random(25) + 97) ; lowercase a to z
Case 3
LongKey$ + Mid("#$%&+/<=>?@\~", Random(12), 1) ; special chars
EndSelect
Next
For X = 1 To KeyLen
PassKey$ + Mid(LongKey$, Random(999) + 1, 1)
Next
ProcedureReturn PassKey$
EndProcedure
For i = 1 To 10
Debug KeyGen(10)
Next i
Jack

Last edited by JackWebb on Mon Jul 20, 2015 7:08 pm, edited 1 time in total.
Make everything as simple as possible, but not simpler. ~Albert Einstein
-
- Always Here
- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: How to generate a number of unique/random strings?
Nice one Jack, thanks for sharing.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
Re: How to generate a number of unique/random strings?
You should really use CryptRandom() for such purposes. The output of Random() can be predictable.
quidquid Latine dictum sit altum videtur
-
- Always Here
- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: How to generate a number of unique/random strings?
Good advice, thanks freak.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
Re: How to generate a number of unique/random strings?
slight adjustment to Little John's code to ensure unique strings
Code: Select all
Procedure.s RandomString (numChars.i, allowedCharacters$="ABCabc123")
; -- works in ASCII mode and Unicode mode
Static NewMap unique.b()
Protected *sourceBase, *source.Character, *dest.Character
Protected lastAllowed.i=Len(allowedCharacters$)-1
Protected ret$ = Space(numChars)
*sourceBase = @allowedCharacters$
*dest = @ret$
While *dest\c <> 0
*source = *sourceBase + Random(lastAllowed) * SizeOf(Character)
*dest\c = *source\c
*dest + SizeOf(Character)
Wend
; -- check that generated string is unique
If FindMapElement(unique(),ret$)
; -- if not, then regenerate it
ret$=RandomString (numChars,allowedCharacters$)
Else
; -- otherwise, add the generated string to the existing 'list'
AddMapElement(unique(),ret$)
EndIf
ProcedureReturn ret$
EndProcedure
there is no sig, only zuul (and the following disclaimer)
WARNING: may be talking out of his hat
WARNING: may be talking out of his hat
-
- Always Here
- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: How to generate a number of unique/random strings?
actually, CryptRandom() delivers uniqueness - good to have a long list of allowed chars too.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
Re: How to generate a number of unique/random strings?
good to knowIdeasVacuum wrote:- good to have a long list of allowed chars too.

there is no sig, only zuul (and the following disclaimer)
WARNING: may be talking out of his hat
WARNING: may be talking out of his hat