Raised This Month: $ Target: $400
 0% 

Setting an unique value to each cell of an array.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
t3hNox
Senior Member
Join Date: Oct 2009
Old 11-25-2010 , 10:33   Setting an unique value to each cell of an array.
Reply With Quote #1

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])
    }


Last edited by t3hNox; 11-25-2010 at 10:38.
t3hNox is offline
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 11-25-2010 , 10:50   Re: Setting an unique value to each cell of an array.
Reply With Quote #2

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])
    }

__________________
Impossible is Nothing
Sylwester is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 11-25-2010 , 14:07   Re: Setting an unique value to each cell of an array.
Reply With Quote #3

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); }
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
t3hNox
Senior Member
Join Date: Oct 2009
Old 11-25-2010 , 14:45   Re: Setting an unique value to each cell of an array.
Reply With Quote #4

Thank you both for help
Everything is working properly.
t3hNox is offline
t3hNox
Senior Member
Join Date: Oct 2009
Old 11-26-2010 , 16:08   Re: Setting an unique value to each cell of an array.
Reply With Quote #5

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.
t3hNox is offline
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 11-26-2010 , 16:57   Re: Setting an unique value to each cell of an array.
Reply With Quote #6

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])
    }

__________________
Impossible is Nothing
Sylwester is offline
t3hNox
Senior Member
Join Date: Oct 2009
Old 11-26-2010 , 17:38   Re: Setting an unique value to each cell of an array.
Reply With Quote #7

I can't really test it right now but thank you for your help.
t3hNox is offline
Reply


Thread Tools
Display Modes

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 11:15.


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