PHP - Generar PNRG sin repetirse

 
Vista:

Generar PNRG sin repetirse

Publicado por aprendiz (12 intervenciones) el 12/03/2006 05:41:54
Hola, tengo un script php para generar passwds al azar, tiene un seed de 1.000.000, , tengo k generar ese millon de passwds, pero no se como ya que obviamente tienden a repetirse al ser "Pseudo aleatorias", sé que se puede pero no sé el metodo, ya intente generando un archivo de mas de 2 millones y luego pasarlo por un programa que borrara los duplicados (duplifind), pero ademas del tiempo k gasta, tan solo llego a los 960.435, tb use el array unique, pero es muy lento!(aunque si funciona) aqui les dejo el codigo, y si alguien tuviera otro metodo y me lo pudiera decir se los agradeceria mucho

<?php
$z = 1;
while ($z <= 100000) {
$z++;
$chars = array('A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g', 'H', 'h', 'I', 'i', 'J', 'j','K', 'k', 'L', 'l', 'M', 'm', 'N', 'n', 'O', 'o', 'P', 'p', 'Q', 'q', 'R', 'r', 'S', 's', 'T', 't', 'U', 'u', 'V', 'v', 'W', 'w', 'X', 'x', 'Y', 'y', 'Z', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0');

$max_chars = count($chars) - 1;
srand( (double) microtime()*1000000);

$rand_str = '';
for($i = 0; $i < 8; $i++)
{
$rand_str = ( $i == 0 ) ? $chars[rand(0, $max_chars)] : $rand_str . $chars[rand(0, $max_chars)];
}

$help = md5($rand_str);
$help2 = substr($help, 0, 6);
$file = "ackkey5.txt";
$handle = fopen($file, "a") or die("could not open file for writing");
fwrite($handle, "$help2\n");
$text = file($file) or die("could not read file");
$unique = array_unique($text);

foreach($unique as $line)
{
fwrite($handle, $line);
}
}

?>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

do it like this.

Publicado por rasmus (1 intervención) el 13/03/2006 03:43:55
<?php
for ( $i=1; $i<=1000000; $i++) {
echo md5($i)."\n";
}

?>
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:do it like this.

Publicado por Aprendiz (12 intervenciones) el 13/03/2006 06:09:23
well it works, and wont repeat, the thing is i need to use the code posted above, that will give me the result i want, thx for the help anyways
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

ok. here you have

Publicado por rasmus (4 intervenciones) el 13/03/2006 17:51:35
ok. here you have.
this is ultrafast
i hope you can learn how to optimize your scripts.
how many passwords?: 100000
how long did it take?: 3 seconds

avegare time, ranged from 3 to 6 seconds. but never more than 6!. wooooww.

<?php
//start timer
$time_start = time();

//don't make it die too soon
set_time_limit(60*60);

//define how many password you want to generate, and their legth
define('TOTAL_PASSWORDS', 100000);
define('PASSWORD_LENGTH', 8);
define('SECRET_SALT', 'fdasdaWEFRWfc87923fsadsf');

//initialize vars
$passwords = array();
$password = '';
$rand = null;

//THE MAIN LOOP
$i = 0;
//trick to check only when needed how many passwors we have so far
while ( $j < TOTAL_PASSWORDS || count($passwords) < TOTAL_PASSWORDS ) {
for($j=0; $j < TOTAL_PASSWORDS; $i++, $j++) {
//generate initial (all lowercase password)
$password = substr(md5(rand().$i.SECRET_SALT), 0, PASSWORD_LENGTH);
//make random uppercase
for ( $k=0; $k < PASSWORD_LENGTH; $k++ ) {
$rand = rand(1, 1000);
//21.8% of probablylity it will be uppercase (leaving out numbers)
if ( $rand <= 218 ) {
$password[$k] = strtoupper($password[$k]);
}
}
//add password as the key to the array (so we don't keep duplicates)
$passwords[$password] = $j;
}
}
//let's flipt it all
$passwords = array_flip($passwords);

//print useful info
echo "how many passwords?: ".count($passwords)."\n";
echo "how long did it take?: ". (time()-$time_start)." seconds\n";

//print passwords
print_r($passwords);

//goodbye
exit;

?>
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:ok. here you have

Publicado por Aprendiz (12 intervenciones) el 15/03/2006 03:56:15
Ok u were right, it was amazingly fast, 3 seconds as promised :D, it has helped me a lot, doesnt repeat thats what i like the most, i never thought of using an array, i thought it would be too slow, iwas very surprised to see how fast it was, thx for helping me out ;), now what i got a doubt about is, ive only seen how an
array with values works, something like this,
"$array = array(1, 2, 4, 8, 16); ", and i know how to select the value from that array,
but i dunno how to select the value of a generated passwd in the array u gave me.
"$passwords = array();", could u plz explain this last thing??
ThX Alot ;) :D:D
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

it looks like you haven't RTFM

Publicado por rasmus (4 intervenciones) el 15/03/2006 21:52:03
http://www.php.net/manual/en/language.types.array.php
http://www.php.net/manual/en/ref.array.php

have you?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Right i havent

Publicado por Aprendiz (12 intervenciones) el 16/03/2006 02:42:38
i did read it, it didnt lead me to anything, tho, will read it again, thx for the help
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar