Domanda

I've got a method that returns a string with predefined length of random alpha numbers.

I just let it run until it comes across a duplicate and breaks out of the loop and display the amount of generations it took before a duplicate was generated.

The loop numbers will be in the millions, and I am wondering if this really is good and efficient of testing it?

È stato utile?

Soluzione

You can use array_search

The function return the string and store it to an array. Then use array_search to see if it is not FALSE and loop.

Altri suggerimenti

Put The generated strings into an array as a key before outputting them.

$array[$alpha_string] = (isset($array[$alpha_string])) ? $array[$alpha_string] + 1 : 1;

Then check which items have a count > 1.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top