AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Creating random array (https://forums.alliedmods.net/showthread.php?t=76040)

v3x 08-17-2008 08:39

Creating random array
 
Okay, I'm extremely tired and I have a question. Let's say I have a 1d array with 26 cells and each cell is filled with a number like so:
PHP Code:

new Array[26]; 
// [0] => 0
// [1] => 1
// [2] => 2
// ...
// [25] => 25 

How could I randomize the entire array without repeating any of the numbers?

danielkza 08-17-2008 08:44

Re: Creating random array
 
Code:

new array[26]
arrayset(array,sizeof(array),-1)

 for(new i=1; i < 27;i++)
{
    new index = random_num(0,sizeof(array)-1))
    if(array[index] == -1)
        array[index] = i
    else
        i--
}

Not tested.

Arkshine 08-17-2008 09:19

Re: Creating random array
 
I hope I understand well what you need. Your array will be filled with random value from 0 to 25. I prefer using another array since it's more fast and easy to do. Another method without using temporary array would be to fill the original array with -1 ( for example ) and each time checking the whole array if the random value is used or not.

Code:
    #include <amxmodx>         public plugin_init ()     {         new MyArray  [ 26 ] = { 0, 1, ... }; // --| Your array { 0, 1, 2, etc.. }         new ArrayTemp[ 26 ];                 new i_Rand;                 for ( new i; i < 26; i++ )         {             i_Rand = random_num ( 0, 25 );                         if ( !ArrayTemp[ i_Rand ] )  // --| This random value is not used             {                 MyArray[ i ] = i_Rand;                 ArrayTemp[ i_Rand ] = 1; // --| This random value is now locked.             }             else // --| This random value is already used, need to try another value             {                 --i;             }         }         // --| Results         for ( new i; i < 26; i++ )         {             log_amx ( "i = %d", MyArray[ i ] );         }     }

v3x 08-17-2008 09:24

Re: Creating random array
 
Thanks, guys :)

Arkshine 08-17-2008 09:34

Re: Creating random array
 
Uh.

You know, you are totaly out of topic... :lol:

Lee 08-17-2008 12:33

Re: Creating random array
 
I'm a little confused. I was under the impression this was an elementary problem. *shrug*

Here was me thinking for loops were intended to iterate a predetermined (whether constant or variable) number of times. If you do insist on using the wrong keyword, there is no point endlessly increasing and decreasing the loop counter.

Code:
for(new i=1; i < 27; ) {     new index = random_num(0, sizeof(array)-1))     if(array[index] == -1)         array[index] = i++ }
I wrote what you're doing in VB.NET (because I'd forgotten how disgusting it was) and these are the number of iterations it took.

172 52 99 63 197 88 109 103 95 54 127 88 68 74 107 138 63 111 123 76

I have a suspicion that the .NET pseudorandom number generator is better in terms of uniform distribution than random_num() and so you might find it takes longer.

The way I'd achieve this is to swap random elements around (you can XOR the values if you're partial to that sort of thing). You can either call random_num() twice per swap or use modulus. It's up to you to decide how many iterations is sufficient. I'm sure you don't need me to code an example. You've probably settled for what's above anyway. :)

Exolent[jNr] 08-17-2008 13:32

Re: Creating random array
 
This might work well.

Code:
new my_array[26]; for( new i = 0; i < sizeof(my_array); i++ ) {     my_array[i] = i; } new rand, temp; for( new i = 0; i < sizeof(my_array); i++ ) {     rand = random(sizeof(my_array));     if( rand == i )     {         continue;     }         temp = my_array[i];     my_array[i] = my_array[rand];     my_array[rand] = temp; }

ConnorMcLeod 08-17-2008 13:41

Re: Creating random array
 
Thie code calculates 78 times sizeof(my_array) lol

Exolent[jNr] 08-17-2008 14:10

Re: Creating random array
 
Mine only iterates through my_array twice.
1. Set all slots to 0-25.
2. Swap each slot with a random one.

EDIT: connorr, I see what you mean.

Lee 08-17-2008 14:14

Re: Creating random array
 
Connorr, can you work out what I'm trying to tell you?

Code:
new var[5]; public entryPoint() {     sizeof var;     sizeof(var);     5; }
Code:

amxxdump -d unknown.amxx

0x8                        PROC              ; public entryPoint()
0xC                      BREAK
0x10                      BREAK
0x14                  CONST.pri  0x5
0x1C                      BREAK
0x20                  CONST.pri  0x5
0x28                      BREAK
0x2C                  CONST.pri  0x5
0x34                  ZERO.pri
0x38                      RETN

My first post in this thread means I'm starting to fall into the trap of caring what colour the bike shed is painted - a problem that has been annoying me recently.


All times are GMT -4. The time now is 03:08.

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