How to generate a number of unique/random strings?

Just starting out? Need help? Post your questions and find answers here.
IdeasVacuum
Always Here
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?

Post by IdeasVacuum »

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()
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Little John
Addict
Addict
Posts: 4775
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: How to generate a number of unique/random strings?

Post by Little John »

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. :-)

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.
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: How to generate a number of unique/random strings?

Post by Tenaja »

This does the easy part:

Code: Select all

For i = 1 To 10
	s$ + Chr(Random(126,32))
Next
Debug s$
This shows one way to eliminate a stretch of characters.

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$
User avatar
Zebuddi123
Enthusiast
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?

Post by Zebuddi123 »

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. :)

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
IdeasVacuum
Always Here
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?

Post by IdeasVacuum »

whoosh! thanks guys :D
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
JackWebb
Enthusiast
Enthusiast
Posts: 109
Joined: Wed Dec 16, 2009 1:42 pm
Location: Tampa Florida

Re: How to generate a number of unique/random strings?

Post by JackWebb »

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!

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
Good Luck!
Jack 8)
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
IdeasVacuum
Always Here
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?

Post by IdeasVacuum »

Nice one Jack, thanks for sharing.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: How to generate a number of unique/random strings?

Post by freak »

You should really use CryptRandom() for such purposes. The output of Random() can be predictable.
quidquid Latine dictum sit altum videtur
IdeasVacuum
Always Here
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?

Post by IdeasVacuum »

Good advice, thanks freak.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
citystate
Enthusiast
Enthusiast
Posts: 638
Joined: Sun Feb 12, 2006 10:06 pm

Re: How to generate a number of unique/random strings?

Post by citystate »

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
IdeasVacuum
Always Here
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?

Post by IdeasVacuum »

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.
citystate
Enthusiast
Enthusiast
Posts: 638
Joined: Sun Feb 12, 2006 10:06 pm

Re: How to generate a number of unique/random strings?

Post by citystate »

IdeasVacuum wrote:- good to have a long list of allowed chars too.
good to know :D
there is no sig, only zuul (and the following disclaimer)

WARNING: may be talking out of his hat
Post Reply