Genera strings aleatorias entre dos rango de caracteres ascii
Para que sirve?
Para crear contraceñas o claves
Como hace?
uso como seed la division "timer" (ni idea que es eso, me lo dijo un amigo y no se de donde sale ese numero.) por la hora del sistema.
despues uso rnd para "fabricar" un numero y a eso lo multiplica por la diferencia entre el maximo valor ascii y el minimo mas uno. Al resultado se le suma el minimo valor ascii y despues se convierte a caracter con la funcion chr
Codigo mRndString
Código
'/-------------------------------------------------------------------------------------\ '|=====================================================================================| '|=/---------------------------------------------------------------------------------\=| '|=| Module : mRndString |=| '|=| DateTime : 17/07/2010 07:42 PM |=| '|=| Author : Elemental Code |=| '|=| Mail : Elemental_Code[at]hotmail[dot]com |=| '|=| WebPage : I haven't got one Yet ^.^ |=| '|=| Purpose : Generates Randoms Strings (like a Password) |=| '|=| Requirements: Two hands (I havent tried with one, it migth work, I don't know) |=| '|=| Distribution: You can freely use this code in your own |=| '|=| applications, but you may not reproduce |=| '|=| or publish this code on any web site, |=| '|=| online service, or distribute as source |=| '|=| on any media without express permission. |=| '|=\---------------------------------------------------------------------------------/=| '|=====================================================================================| '\-------------------------------------------------------------------------------------/ Option Explicit Public Function Random_String(StringLength As Long) As String Dim FinalString As String Dim MaxAscii As Integer Dim MinAscii As Integer Dim CurLength As Long 'ENG: 48 to 126 All characters | 65 to 126 just letters |48 to 57 Numbers Only | for more info get an ascii table. >.< 'ESP: 48 al 126 Todos los caracteres | 65 al 126 solo letras | 48 al 57 Solo Numeros | Para mas info buscate una table ascii >.< MaxAscii = 126 MinAscii = 48 'ENG: to make it as random as I can the number changes each milisecond. 'ESP: para hacerlo lo mas aleatorio posible el numero cambia cada milisegundo. Randomize Timer / Val(Time) For CurLength = 1 To StringLength FinalString = FinalString & Chr(Int((MaxAscii - MinAscii + 1) * Rnd + MinAscii)) Next CurLength Random_String = StrReverse(FinalString) End Function
Proyecto de ejemplo
Pongan en un form 2 textbox y un comand button
el siguiente codigo va en el codigo del boton
(la caja 1 sera la string generada y la caja 2 sera la longitud)
Código
Text1.Text = Random_String(Text2.Text)
Espero que sirva ^.^