Home NetLinx Studio

Random numbers

Dear All,

  1. I need to randomly select 10 numbers from 1-100 and store them in an array. Random_number(101) selects a number in range 0 <= number < 101 but zero must be excluded.

I could make some piece of code to check if the selected number is zero:

for (iNumber= 1 ; iNumber <=10 ; iNumber ++)
{
iNumberArray[iNumber] = random_number(101);

    while ( iNumberArray[iNumber] == 0 )    
            iNumberArray[iNumber] = random_number(101);   
}   
  1. I need then to check these 10 numbers to be different for each other. If there are equal numbers, it should re select different ones

  2. Finally I need to short them from the lower to the higher number

Basically, I’d like to ask some help for 2 and 3.

Thanks,
George

Comments

  • richardhermanrichardherman not-so-junior member Posts: 239

    You could do something like this. I have checked this quickly and it works as expected, but depending on how 'paranoid' you are, it could be worth checking this more thorough.

    DEFINE_CONSTANT
    
    MAX_NUMBERS = 10
    
    DEFINE_VARIABLE
    
    volatile long lNumberArray[MAX_NUMBERS]
    volatile long lSortedArray[MAX_NUMBERS]
    

    Functions:

    1 - generate the array

    define_function fnFillArray()
    {
        stack_var volatile integer i
    
        for (i = 1 ;i <= MAX_NUMBERS ;i ++)
        {
        //set max range 1 lower and add 1 to avoid zero
        lNumberArray[i] = random_number(100) + 1
        set_length_array(lNumberArray, MAX_NUMBERS)
        }   
    }
    

    You can check the array with debug. You can change some array members to be duplicates of others there also, to check the next function.

    2 - check for duplicates

    define_function fnFindDuplicates(long lArray[])
    {
        stack_var volatile integer i
        stack_var volatile integer j
        stack_var volatile integer nDuplicate
    
        while (TRUE)
        {
        //after replacing a duplicate check again
        //cause it could be another duplicate
        for (i = 1; i <= length_array(lArray); i++)
        {
            for (j = i + 1; j <= length_array(lArray); j++) 
            {
            if (lArray[i] == lArray[j]) 
            {
                send_string 0,"'Duplicate: ',itoa(lArray[i])"
                //re-generate and check array again
                lNumberArray[i] = random_number(100) + 1
                nDuplicate = i
                break
            }
            else
                nDuplicate = FALSE
            }
            if (nDuplicate)
            break
        }
        if (!nDuplicate)
        {
            send_string 0,'Duplicate checking done!'
            return
        }
        }
    }
    

    3 - Sort the array

    This is a 'standard' insertion sort routine. If you want to know how it works, just Google. Lots of examples.

    define_function long[MAX_NUMBERS] fnSortArray(long lArray[])
    {
        //insertion sort routine
    
        stack_var volatile integer i
        stack_var volatile long    lValueToInsert
        stack_var volatile integer nHoleposition
        stack_var volatile long    lTempArray[MAX_NUMBERS]
    
        lTempArray = lArray
    
        for (i = 2; i <= length_array(lTempArray); i++) 
        {
        lValueToInsert = lTempArray[i]
        nHoleposition = i
        while (lValueToInsert < lTempArray[nHoleposition - 1]) 
        {
            lTempArray[nHoleposition] = lTempArray[nHoleposition - 1]
            nHoleposition --
            if (nHoleposition == 1) 
            {
            break
            }
        }
        lTempArray[nHoleposition] = lValueToInsert
        }
        send_string 0,'Sorting done!'
        return lTempArray
    }
    

    I tested these functions with simple triggers from pulsing channels on a virtual device with 'control a device' and checking the results in 'debug' but there are may ways of doing this.

    DEFINE_DEVICE
    
    vdvStart = 33000:1:0
    
    DEFINE_EVENT
    
    channel_event [vdvStart,1]
    {
        ON:
        {
        fnFillArray()
        }
    }
    
    channel_event [vdvStart,2]
    {
        ON:
        {
        fnFindDuplicates(lNumberArray)
        }
    }
    
    channel_event [vdvStart,3]
    {
        ON:
        {
        lSortedArray = fnSortArray(lNumberArray)
        }
    }
    

    Good luck

  • George KrietsepisGeorge Krietsepis Member Posts: 180

    hello richardherman,

     they are all so useful..... I will give a try.... Many thanks for your help and your time !!!
    

    George

Sign In or Register to comment.