AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Setting an unique value to each cell of an array. (https://forums.alliedmods.net/showthread.php?t=143823)

t3hNox 11-25-2010 10:33

Setting an unique value to each cell of an array.
 
Hi. It is probably an easy thing to script but I have some problems with the code. See below:
PHP Code:

#include <amxmodx>

#define SIZE 10

new var[SIZE]
new 
iRandNum

public plugin_init()
{
    
register_plugin("Random Array Values""v0.1""t3h")
    
register_clcmd("say /print""cmdPrint")
}

public 
cmdPrint(id)
{
    for(new 
i=0SIZEi++)
    {
        
iRandNum random(SIZE)+1
        
        
for(new 0<= ij++)
        {
            if(var[
j] == iRandNum)
            {
                
//here I need to repeat the first loop so that there would be another random number
            
}
        }
        
        var[
i] = iRandNum
        client_print
(idprint_console"Number: %d", var[i])
    }



Sylwester 11-25-2010 10:50

Re: Setting an unique value to each cell of an array.
 
try this:
PHP Code:

public cmdPrint(id)
{
    for(new 
i=0SIZEi++)
    {
        var[
i] = i+1
    
}
    new 
tmprand
    
for(new i=0SIZE-1i++)
    {
        
rand i+random(SIZE-i)

        
tmp = var[i]
        var[
i] = var[rand]
        var[
rand] = tmp       
    
}
    for(new 
i=0SIZEi++)
    {
        
client_print(idprint_console"var[%d]: %d"i, var[i])
    }



Exolent[jNr] 11-25-2010 14:07

Re: Setting an unique value to each cell of an array.
 
Code:
public cmdPrint(id) {     new tmp[SIZE];     for(new i = 0; i < SIZE; i++)     {         tmp[i] = i + 1;     }     new rand, tsize = SIZE;     for(new i = 0; i < SIZE; i++)     {         rand = random(tsize);         var[i] = tmp[rand];         tmp[rand] = tmp[--tsize];         client_print(id, print_console, "Number: %d", var[i]);     } }
or
Code:
public cmdPrint(id) {     for(new i = 0; i < SIZE; i++)     {         var[i] = i + 1;     }         SortCustom1D(var, SIZE, "SortRandomVars");         for(new i = 0; i < SIZE; i++)     {         client_print(id, print_console, "Number: %d", var[i]);     } } public SortRandomVars(elem1, elem2, array[], data[], size) {     return clamp(random_num(-50, 50), -1, 1); }

t3hNox 11-25-2010 14:45

Re: Setting an unique value to each cell of an array.
 
Thank you both for help :)
Everything is working properly.

t3hNox 11-26-2010 16:08

Re: Setting an unique value to each cell of an array.
 
Sorry for double posting but I have another question related to this thread.
I want to make so that at the end there would be printed 8 numbers (defined with NUMSTOPRINT) in the client's console but none of them would be the one that holds AnotherRandVar value. Hope you understand what I mean.
PHP Code:

#include <amxmodx>

#define SIZE 10
#define NUMSTOPRINT 8

new var[SIZE]

public 
plugin_init()
{
    
register_plugin("Random Array Values""v0.1""thh33")
    
register_clcmd("say /print""cmdPrint")
}

public 
cmdPrint(id)
{
    new 
tmp[SIZE];
    for(new 
0SIZEi++)
    {
        
tmp[i] = 1;
    }
    new 
randtsize SIZE;
    for(new 
0SIZEi++)
    {
        
rand random(tsize);
        var[
i] = tmp[rand];
        
tmp[rand] = tmp[--tsize];
    }
    
    
//SEE FROM HERE:
    
new AnotherRandVar random(SIZE)+1
    
for(new i=0i<=NUMSTOPRINTi++)
    {
        if(var[
i] == AnotherRandVar)
        {
            
//don't print this value, print another one
        
}
        
client_print(idprint_console"Num: %d", var[i])
    }


So at the end there will be printed 8 different numbers and none of them is AnotherRandVar.

Sylwester 11-26-2010 16:57

Re: Setting an unique value to each cell of an array.
 
this should work (assuming that there is only 1 value equal to AnotherRandVar):
PHP Code:

public cmdPrint(id)
{
    
//...
    //SEE FROM HERE:
    
new AnotherRandVar random(SIZE)+1
    
new offset
    
for(new i=0i<NUMSTOPRINTi++)
    {
        if(var[
i] == AnotherRandVar)
        {
            
offset++
        }
        
client_print(idprint_console"Num: %d", var[i+offset])
    }



t3hNox 11-26-2010 17:38

Re: Setting an unique value to each cell of an array.
 
I can't really test it right now but thank you for your help.


All times are GMT -4. The time now is 11:15.

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