Raised This Month: $ Target: $400
 0% 

Random numbers without repeating


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 05-02-2016 , 14:33   Re: Random numbers without repeating
Reply With Quote #6

It depends on implementation.
If it actually is 5 values between 1 and 10 it doesn't matter.
But your method of scrambling an array will be more consistent as the previous 2 versions of pure randomizing values will be dependent on the availability of the numbers. As you fill the array and the available numbers become rare, time increases because of retries.

Here's a small example comparing the two functions.
Code:
#include <amxmodx> #include <rose> #define SIZE 5 #define MIN 1 #define MAX 10 #define RANDOMIZER 10 public plugin_init() {     register_plugin("Test Plugin 5", "", "[ --{-@ ]");     server_print("MIN: %d, MAX: %d, SIZE: %d, RANDOMIZER: %d", MIN, MAX, SIZE, RANDOMIZER);         new hTimer = TimerStart();     for ( new l ; l < 1000000 ; l++ )         Randomize_1()     TimerStop(hTimer);     new output[32];     TimerFormat(hTimer, output, charsmax(output), 2)     server_print("Method one, 1 000 000 runs, time: %s", output);     hTimer = TimerStart();     for ( new l ; l < 1000000 ; l++ )         Randomize_2()     TimerStop(hTimer);     TimerFormat(hTimer, output, charsmax(output), 2)     server_print("Method two, 1 000 000 runs, time: %s", output); } Randomize_1() {     static num[SIZE];     for ( new i ; i < sizeof num ; i++ ) {         do num[i] = random_num(MIN, MAX);         while ( AlreadyExists(num[i], num, i) )     } } AlreadyExists(num, array[], size) {     for ( new i ; i < size ; i++ )         if ( array[i] == num )             return 1;     return 0; } Randomize_2() {     static num[SIZE];     static values[MAX];     new temp, r;     for ( new i ; i < sizeof values ; i++ )         values[i] = i + 1;     for ( new i ; i < RANDOMIZER ; i++ ) {         r = random(sizeof values);         temp = values[i % sizeof values];         values[i % sizeof values] = values[r];         values[r] = temp;     }     for ( new i ; i < sizeof num ; i++ )         num[i] = values[i]; }

Here are some examples I ran:
Code:
MIN 1, MAX 10, SIZE 5, RANDOMIZER 10:
Method one, 1 000 000 runs, time: 1s 853ms
Method two, 1 000 000 runs, time: 2s 681ms
Code:
MIN: 1, MAX: 10, SIZE: 5, RANDOMIZER: 30
Method one, 1 000 000 runs, time: 1s 882ms
Method two, 1 000 000 runs, time: 5s 930ms
Code:
MIN: 1, MAX: 5, SIZE: 5, RANDOMIZER: 10
Method one, 1 000 000 runs, time: 2s 960ms
Method two, 1 000 000 runs, time: 2s 428ms
Code:
MIN: 1, MAX: 20, SIZE: 15, RANDOMIZER: 10
Method one, 1 000 000 runs, time: 12s 618ms
Method two, 1 000 000 runs, time: 3s 685ms
Code:
MIN: 1, MAX: 20, SIZE: 20, RANDOMIZER: 50
Method one, 1 000 000 runs, time: 41s 402ms
Method two, 1 000 000 runs, time: 10s 470ms
As you can see, as the size increases and the possible values become more similar to the number of slots to fill the first method gets really slow.
Again, this is 1 000 000 runs. So even 41s equals to an average of .041 milliseconds per run. Again, trivial.
__________________

Last edited by Black Rose; 05-02-2016 at 15:10.
Black Rose is offline
 



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 18:43.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode