www.ClassicTW.com
http://www.classictw.com/

RANDOM AND ARRAY
http://www.classictw.com/viewtopic.php?f=15&t=21667
Page 1 of 2

Author:  Yop_Solo [ Sun Mar 15, 2009 2:59 am ]
Post subject:  RANDOM AND ARRAY

There is my question.
The goal is to randomize all values in my array without having more than 5 times the same value.

example :
array[1] = 3
array[2] = 2
array[3] = 3
array[4] = 2
array[5] = 1
array[6] = 1
array[7] = 3
array[8] = 3
array[9] = 3
array[10] = 2

So this example, there are 5 times a "3" , 3 times a "2" and 2 times "1".
So I want to randomize each array but I don't want to have more than 5 times the same value...
How you would do it ?

Author:  Singularity [ Sun Mar 15, 2009 3:30 am ]
Post subject:  Re: RANDOM AND ARRAY

You need to look up randomizer algorithms. There are numerous approaches to this, selection randomization, insertion randomization, shuffle randomizing, jiggle randomizers, combinations there-of. All of them have important computational and memory costs. All of which depends entirely on the data set you're using and how you're using it.

Author:  Yop_Solo [ Sun Mar 15, 2009 3:38 am ]
Post subject:  Re: RANDOM AND ARRAY

Ok... so where I can find those algorithme cause I need to do this... memory isn't so bad cause i will not run always that script, it will only be run one time in a game and it will be at the start...

Author:  Vid Kid [ Sun Mar 15, 2009 3:52 am ]
Post subject:  Re: RANDOM AND ARRAY

SetVar $array[1] 3
SetVar $array[2] 2
SetVar $array[3] 3
SetVar $array[4] 2
SetVar $array[5] 1
SetVar $array[6] 1
SetVar $array[7] 3
SetVar $array[8] 3
SetVar $array[9] 3
SetVar $array[10] 2

:Loop
GetRnd $Check 1 10
Echo ANSI_10 "*" $array[$check]
SetVar $value $array[$check]
IF ($Value = 1)
Add $1count 1
ElseIF ($Value = 2)
Add $2count 1
ElseIF ($Value = 3)
Add $3count 1
END

IF ($1count <> 5)
Else
SetVar $winner 1
Goto :end
End
IF ($2count <> 5)
Else
SetVar $winner 2
Goto :end
End
IF ($3count <> 5)
Else
SetVar $winner 3
Goto :end
End
Goto :loop
Halt

:end
Echo ANSI_10 "**We have 5 "& $winner &"'s*"

Author:  Singularity [ Sun Mar 15, 2009 5:16 am ]
Post subject:  Re: RANDOM AND ARRAY

Quote:
Ok... so where I can find those algorithme cause I need to do this... memory isn't so bad cause i will not run always that script, it will only be run one time in a game and it will be at the start...


You won't find them written in twxproxy scripting. Basically you have to think of them conceptually and work downward.

An easy variant is to use to a list and just write your values to the list, use getRnd to select one and replaceText to remove the values once selected.

But all of that's going to go to application.

For instance a selection randomizer would randomly get one of the items from the list, add it to another list, and remove the original instance. That would build a randomized list and depending on the entropy required you can run multiple passes.

An insertion randomizer goes down a list incrementally and randomly inserts into the 2nd array. A shuffle-style splits the array into 2 chunks, goes down the first array and randomly inserts each in between 2 items on the 2nd array. A jiggle algo goes down the list incrementally and randomly decides whether or not to flip 2 numbers, causing items to move in little amounts randomly.

So that brings us to the "depends on what you're doing" thing.

First, do you already have the array populated with all of the instances, or do you need to populate the list first? Is "5 times" a fixed number or is it more like "N times" where N could be any given number? If you select from the list, do you want to remove that number from the list or just that instance of that number from the list? Or do you not want it removed at all, ie: the probability of selecting any given instance doesn't change over time and isn't conditional.

Or do you just have a generic array, that could be anything, and you simply want to randomize the array? Are the array values fixed to numbers, or could it be strings as well? Are the numbers sector numbers, or do the have other bounding? Are the array indexes all numeric, or do you use label indexes anywhere?

Author:  Kaus [ Sun Mar 15, 2009 11:00 am ]
Post subject:  Re: RANDOM AND ARRAY

This is my solution to the above example. There is more than likely a way to write it that will use less lines. I'm not sure if or what algorithim my solution follow's but it does spit out 10 randomly generated numbers with nomore than 5 of the same number.
Code:
#-----Initilize our Variables
SetVar $Idx 1
SetVar $Count1 0
SetVar $Count2 0
SetVar $Count3 0
#---------Start the loop
While ($Idx <= 10)
GetRND $RandomNum 1 3
  SetVar $Test[$Idx] $RandomNum
#----------Increment Counters
If ($Test[$Idx] = 1)
  Add $Count1 1
ElseIf ($Test[$Idx] = 2)
  Add $Count2 1
ElseIf ($Test[$Idx] = 3)
  Add $Count3 1
end
Add $Idx 1
#-------Reconcile The Counters
If ($Count1 = 6)
  Subtract $Idx 1
  subtract $Count1 1
ElseIf ($Count2 = 6)
  Subtract $Idx 1
  Subtract $Count2 1
ElseIf ($Count3 = 6)
  Subtract $Idx 1
  Subtract $Count3 1
end
#--------End our loop
end
#--Echo the Results of the loop
SetVar $Idx 1
While ($Idx <= 10)
Echo ANSI_15 $Test[$Idx]
Add $Idx 1
end

Author:  Yop_Solo [ Sun Mar 15, 2009 7:00 pm ]
Post subject:  Re: RANDOM AND ARRAY

Thanks for the answer,

i will try to incorporate your script or the way of thinking in my main script and it should work.

Thanks again.

Author:  LoneStar [ Sun Mar 15, 2009 8:09 pm ]
Post subject:  Re: RANDOM AND ARRAY

Here's my suggestion..
Code:
setarray $DATA SECTORS 1
setvar $LIMIT 5
setVar $IDX 1
while ($IDX <= SECTORS)
   getrnd $RNDDAT 1 SECTORS
   while ($DATA[$RNDDAT][1] >= $LIMIT)
      getrnd $RNDDAT 1 SECTORS
   end
   setvar $DATA[$IDX] $RNDDAT
   add $DATA[$RNDDAT][1] 1
   add $IDX 1
end

Review Results...
Code:
setvar $FILE "c:\blah.txt"
DELETE $FILE
setvar $IDX 1
while ($IDX <= sectors)
   write $FILE ("Value: " & $IDX & ", Times Used: " & $DATA[$IDX][1])
   add $idx 1
end


EDIT: I know Yop is happy with the help recieved so far. Just want to see if I could solve the problem in 10lines or less..

Author:  Promethius [ Sun Mar 15, 2009 8:42 pm ]
Post subject:  Re: RANDOM AND ARRAY

hmm, thought I had a fairly short version, but will have to check it better. ;)

k, major issue with the below code. Can you find it?

Code:
setVar $i 1
setVar $minValue 1
setVar $maxValue 3
while ($i <= 10)
  :loop
  getRnd $rndValue $minValue $maxValue
  add $chk[$rndValue] 1
  if ($chk[$rndValue] <= 4)
      setVar $array[$i] $rndValue
  else
     goto :loop
  end
  add $i 1
end

  #### display ####
setVar $i 1
while ($i <= 10)
   echo ANSI_12 "*Array[" & $i & "] value is: " $array[$i]
   add $i 1
end


Author:  LoneStar [ Sun Mar 15, 2009 9:59 pm ]
Post subject:  Re: RANDOM AND ARRAY

Ok. Had little more key time so here's my final attempt (far more practicle)
Code:
setarray $DATA 10 1
setvar $LIMIT 5
setvar $UPPER 5
setVar $LOWER 1
setVar $IDX 1
while ($IDX <= 10)
:BRANCH_HERE
getrnd $RNDDAT $LOWER $UPPER
Branch ($DATA[$RNDDAT][1] <= $LIMIT) :BRANCH_HERE
setvar $DATA[$IDX] $RNDDAT
add $DATA[$RNDDAT][1] 1
add $IDX 1
end
#Spits out the results
setvar $IDX 1
setVar $STR1 ""
setVar $STR2 ""
while ($IDX <= 10)
setvar $STR1 ($STR1&"*"&ANSI_15&"Array Element: "&ANSI_12&$IDX&ANSI_15&", Value: "&ANSI_12&$DATA[$IDX])
if ($IDX <= $UPPER)
setvar $STR2 ($STR2&"*"&ANSI_15&"Value: "&ANSI_8&$IDX&ANSI_15&", Times Used: "&ANSI_8&$DATA[$IDX][1])
end
add $idx 1
end
Echo "**" & $STR1 & "**" & $STR2 & "**"

Author:  Vid Kid [ Mon Mar 16, 2009 6:57 am ]
Post subject:  Re: RANDOM AND ARRAY

Yup Promethius
I think I found it , you didnt incroment your while counter within the :loop lable , so script would probly jam cpu

of course I didnt try it .. I'm just going by my first read of it.

Let me know if I missed something :)

Author:  Yop_Solo [ Mon Mar 16, 2009 9:43 pm ]
Post subject:  Re: RANDOM AND ARRAY

LoneStar,

With the Vid script, I made my own to fit in my script. And you know what, it seems that you think the way I put it... but you showed me a little thing that I will need to add in mine so... It was very nice to help me.

Also, do you know what is mean Yop Solo ?

Author:  Kaus [ Mon Mar 16, 2009 11:57 pm ]
Post subject:  Re: RANDOM AND ARRAY

Promethius wrote:
hmm, thought I had a fairly short version, but will have to check it better. ;)

k, major issue with the below code. Can you find it?

Code:
setVar $i 1
while ($i <= 10)
:loop 
  getRnd $rndValue 1 3
   add $chk[$rndValue] 1
    if ($chk[$rndValue] < 4)
      setVar $array[$i] $rndValue
  else
     goto :loop
  end
  add $i 1
end

  #### display ####
setVar $i 1
while ($i <= 10)
   echo ANSI_12 "*Array" $i  " value is: " $array[$i]
   add $i 1
end


Not sure if my corrections address the "error", also it doesnt check for duplicates of 5 or more at least that was my understanding of his question.

Author:  Promethius [ Tue Mar 17, 2009 12:06 am ]
Post subject:  Re: RANDOM AND ARRAY

Vid Kid wrote:
Yup Promethius
I think I found it , you didnt incroment your while counter within the :loop lable , so script would probly jam cpu

of course I didnt try it .. I'm just going by my first read of it.

Let me know if I missed something :)


You caught part of it because there is no safety in the loop to break it.

As long as the numbers are setup the way they are, you would be fine. But if you change ($i <= 10) to ($i <= 15), you will run into an infinite loop because the 3 random values you have (minValue 1 to maxvalue 3) will max out at 5 occurences, and "If ($chk[$rndValue] <= 4) would always be false and go to the loop.

Author:  Vid Kid [ Tue Mar 17, 2009 1:18 am ]
Post subject:  Re: RANDOM AND ARRAY

I know what he was doing with the script before hand and explained in a email and posted on EIS so others may learn.

The example I put up is ok , but the real reason I wrote it that way is because I know
he wasn't really going to use the NUMBERS 1 , 2 & 3 as his compair values

That is why I wrote it the way I did , easyer to exchange HIS values for the example
I provided.

Everyone else has been writing script to compair value 1 , 2 & 3 as if they were going to be numbers instead of words.

Good work to all that provided examples .. in the long run I'm sure others will get something from this.

Page 1 of 2 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/