Pogledajte određenu poruku
Staro 04. 02. 2006.   #33
Petar Marić
Python Ambassador
Master
 
Avatar Petar Marić
 
Datum učlanjenja: 06.06.2005
Lokacija: Novi Sad
Poruke: 602
Hvala: 28
27 "Hvala" u 17 poruka
Petar Marić će postati "faca" uskoro
Pošaljite ICQ poruku za Petar Marić
Lightbulb

OK, na kraju sam i ja napravio rešenje - doduše u MATLAB-u.

generate_unique_codes.m:
Kôd:
function false_hit_chance = generate_unique_codes(number_of_codes, percent_of_extra_codes, out_file)
%Generates random unique 8 character codes
%
%INPUT ARGUMENTS:
%   number_of_codes         - desired number of codes
%   percent_of_extra_codes  - percent of extra generated codes.
%                             A non-zero value might be needed to eliminate nonunique codes
%   out_file                - file to which the codes will be outputed
%OUTPUT:
%   false_hit_chance        - odds of a false hit
%NOTES:
%   The codes themselves are a hexadecimal representation of randomly generated 32 bit integers
%   The odds of a false hit are number_of_codes/2^32

maximum_number_of_codes = 2^32; %do not change this or the code wont work

if number_of_codes > maximum_number_of_codes
    error('ERROR: Unable to generate desired number of codes! Try decrasing the value of number_of_codes')
end
if percent_of_extra_codes < 0
    error('ERROR: percent_of_extra_codes must be a non-negative number! Try increasing the value of percent_of_extra_codes')
end

codes = unique(  randint(1, round( number_of_codes*(1 + percent_of_extra_codes/100) ), maximum_number_of_codes)  );

length_of_generated_codes = length(codes);
if length_of_generated_codes < number_of_codes
    error('ERROR: Failed to generate the desired number of codes! Try increasing the value of percent_of_extra_codes parameter.')
end

codes(number_of_codes+1:length_of_generated_codes) = []; %erase the unwanted codes

fp = fopen(out_file, 'w');
fprintf(fp, '%08x\n', codes);
fclose(fp);

false_hit_chance = number_of_codes/maximum_number_of_codes;
test.m:
Kôd:
out_file = 'out.txt';
test_cases = [ 1e2  0.1
               1e3  0.1
               1e4  0.1
               1e5  0.1
               1e6  0.1
               2e6  0.1 ];


%%%% DO NOT EDIT BELOW THIS LINE %%%%
number_of_tests = size(test_cases, 1);
tmp = sprintf('Starting the test with %d test case(s)...\n', number_of_tests);
disp(tmp)

for i = 1:number_of_tests
    number_of_codes         = test_cases(i, 1);
    percent_of_extra_codes  = test_cases(i, 2);

    tmp = sprintf('TEST %3d: CALLING generate_unique_codes(%d, %g, \"%s\")', i, number_of_codes, percent_of_extra_codes, out_file);
    disp(tmp)
    start_time = clock;
    
    generate_unique_codes(number_of_codes, percent_of_extra_codes, out_file);
    
    stop_time = clock;
    elapsed_time = etime(stop_time, start_time);
    tmp = sprintf('TEST %3d was done in %g second(s).\n', i, elapsed_time);
    disp(tmp)
end

disp('All test cases were successfully completed.')
Rezultati testa:
Kôd:
>> test
Starting the test with 6 test case(s)...

TEST   1: CALLING generate_unique_codes(100, 0.1, "out.txt")
TEST   1 was done in 0.016 second(s).

TEST   2: CALLING generate_unique_codes(1000, 0.1, "out.txt")
TEST   2 was done in 0 second(s).

TEST   3: CALLING generate_unique_codes(10000, 0.1, "out.txt")
TEST   3 was done in 0.031 second(s).

TEST   4: CALLING generate_unique_codes(100000, 0.1, "out.txt")
TEST   4 was done in 0.375 second(s).

TEST   5: CALLING generate_unique_codes(1000000, 0.1, "out.txt")
TEST   5 was done in 3.766 second(s).

TEST   6: CALLING generate_unique_codes(2000000, 0.1, "out.txt")
TEST   6 was done in 7.797 second(s).

All test cases were successfully completed.
>>
Naravno svi kodovi su jedinstveni i generiše se tačno zahtevani broj kodova, tj ako se mogu generisati pri zadatim uslovima.

Šta na kraju da kažem do: The speed of my code sparkels your mind
__________________
Python Ambassador of Serbia
Petar Marić je offline   Odgovorite uz citat