The first question you need to ask is whether you really need an identifier to be random. Sometimes consecutive identifiers are good enough.
Now, if you want this to be random, we first notice that the generated sequence of numbers that do not contain duplicates cannot be called randomly .: p Now that we have it fixed, the fastest way to do this is to have a Hashtable or HashMap containing all identifiers already created. Whenever a new identifier is generated, check it on the hash table, recreate if the identifier already exists. This usually works well if the number of students is much less than the number of identifiers. If not, you are experiencing deeper problems, as the likelihood of having to recover an identifier increases, P (generate a new identifier) = number_of_id_already_generated / number_of_all_possible_ids. In this case, check the first paragraph (do you need the ID to be random?).
Hope this helps.
Chris Henry May 19 '10 at 8:26 a.m. 2010-05-19 08:26
source share