Raised This Month: $ Target: $400
 0% 

Creating random array


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
v3x
Veteran Member
Join Date: Oct 2004
Location: US
Old 08-17-2008 , 08:39   Creating random array
Reply With Quote #1

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?
__________________
What am I doing these days? Well, I run my own Rust server. It's heavily modded. If you'd like to join, the ip is 167.114.101.67:28116

I also created a website called Rust Tools. It will calculate and tell you the raw amounts of resources needed to craft items.
v3x is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 08-17-2008 , 08:44   Re: Creating random array
Reply With Quote #2

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.
danielkza is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 08-17-2008 , 09:19   Re: Creating random array
Reply With Quote #3

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 ] );         }     }
__________________
Arkshine is offline
v3x
Veteran Member
Join Date: Oct 2004
Location: US
Old 08-17-2008 , 09:24   Re: Creating random array
Reply With Quote #4

Thanks, guys
__________________
What am I doing these days? Well, I run my own Rust server. It's heavily modded. If you'd like to join, the ip is 167.114.101.67:28116

I also created a website called Rust Tools. It will calculate and tell you the raw amounts of resources needed to craft items.
v3x is offline
Old 08-17-2008, 09:32
AntiBots
This message has been deleted by AntiBots.
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 08-17-2008 , 09:34   Re: Creating random array
Reply With Quote #5

Uh.

You know, you are totaly out of topic...
__________________
Arkshine is offline
Lee
AlliedModders Donor
Join Date: Feb 2006
Old 08-17-2008 , 12:33   Re: Creating random array
Reply With Quote #6

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.
__________________
No support via PM.

Last edited by Lee; 08-17-2008 at 13:08.
Lee is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 08-17-2008 , 13:32   Re: Creating random array
Reply With Quote #7

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; }
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!

Last edited by Exolent[jNr]; 08-18-2008 at 15:27.
Exolent[jNr] is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 08-17-2008 , 13:41   Re: Creating random array
Reply With Quote #8

Thie code calculates 78 times sizeof(my_array) lol
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 08-17-2008 , 14:10   Re: Creating random array
Reply With Quote #9

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.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!

Last edited by Exolent[jNr]; 08-17-2008 at 14:38.
Exolent[jNr] is offline
Lee
AlliedModders Donor
Join Date: Feb 2006
Old 08-17-2008 , 14:14   Re: Creating random array
Reply With Quote #10

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.
__________________
No support via PM.

Last edited by Lee; 08-17-2008 at 14:23.
Lee is offline
Reply



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 03:08.


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