View unanswered posts | View active topics It is currently Fri Dec 26, 2025 7:25 am



Reply to topic  [ 23 posts ]  Go to page 1, 2  Next
 RANDOM AND ARRAY 
Author Message
Gameop
User avatar

Joined: Mon Jan 26, 2009 12:27 am
Posts: 123
Unread post 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 ?

_________________
Dodger known as Yop Solo, Sysop
twgsdodgerbbs.ath.cx
http://cyberquebec.ca/_layout/?uri=http://cyberquebec.ca/twgsdodgerbbs/


Sun Mar 15, 2009 2:59 am
Profile
Veteran Op
User avatar

Joined: Thu Jun 02, 2005 2:00 am
Posts: 5558
Location: USA
Unread post 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.

_________________
May the unholy fires of corbomite ignite deep within the depths of your soul...

1. TWGS server @ twgs.navhaz.com
2. The NavHaz Junction - Tradewars 2002 Scripts, Resources and Downloads
3. Open IRC chat @ irc.freenode.net:6667 #twchan
4. Parrothead wrote: Jesus wouldn't Subspace Crawl.

*** SG memorial donations via paypal to: dpocky68@booinc.com
Image


Sun Mar 15, 2009 3:30 am
Profile ICQ WWW
Gameop
User avatar

Joined: Mon Jan 26, 2009 12:27 am
Posts: 123
Unread post 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...

_________________
Dodger known as Yop Solo, Sysop
twgsdodgerbbs.ath.cx
http://cyberquebec.ca/_layout/?uri=http://cyberquebec.ca/twgsdodgerbbs/


Sun Mar 15, 2009 3:38 am
Profile
Commander

Joined: Sun Feb 25, 2001 3:00 am
Posts: 1838
Location: Guam USA
Unread post 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*"

_________________
TWGS V2 Vids World on Guam Port 2002
Telnet://vkworld.ddns.net:2002
Discord @ DiverDave#8374
Vid's World Discord

Founding Member -=[Team Kraaken]=- Ka Pla

Image
Winners of Gridwars 2010
MBN Fall Tournament 2011 winners Team Kraaken
Undisputed Champions of 2019 HHT Just for showing up!

The Oldist , Longist Running , Orginal Registered Owner of a TWGS server :
Vids World On Guam


Sun Mar 15, 2009 3:52 am
Profile WWW
Veteran Op
User avatar

Joined: Thu Jun 02, 2005 2:00 am
Posts: 5558
Location: USA
Unread post 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?

_________________
May the unholy fires of corbomite ignite deep within the depths of your soul...

1. TWGS server @ twgs.navhaz.com
2. The NavHaz Junction - Tradewars 2002 Scripts, Resources and Downloads
3. Open IRC chat @ irc.freenode.net:6667 #twchan
4. Parrothead wrote: Jesus wouldn't Subspace Crawl.

*** SG memorial donations via paypal to: dpocky68@booinc.com
Image


Sun Mar 15, 2009 5:16 am
Profile ICQ WWW
Gameop
User avatar

Joined: Tue Nov 19, 2002 3:00 am
Posts: 1050
Location: USA
Unread post 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

_________________
Dark Dominion TWGS
Telnet://twgs.darkworlds.org:23
ICQ#31380757, -=English 101 pwns me=-
"This one claims to have been playing since 1993 and didn't know upgrading a port would raise his alignment."


Sun Mar 15, 2009 11:00 am
Profile ICQ
Gameop
User avatar

Joined: Mon Jan 26, 2009 12:27 am
Posts: 123
Unread post 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.

_________________
Dodger known as Yop Solo, Sysop
twgsdodgerbbs.ath.cx
http://cyberquebec.ca/_layout/?uri=http://cyberquebec.ca/twgsdodgerbbs/


Sun Mar 15, 2009 7:00 pm
Profile
Commander
User avatar

Joined: Fri Jun 09, 2006 2:00 am
Posts: 1400
Location: Canada
Unread post 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..

_________________
----------------------------
-= QUANTUM Computing 101: 15 = 3 x 5 ... 48% of the time.
-= There are 10 types of people in the world: Those that understand Binary and those who do not
-= If Oil is made from Dinosaurs, and Plastic is made from Oil... are plastic Dinosaurs made from real Dinosaurs?
-= I like to keep my friends and my enemies rich, and wait to see which is which - Tony Stark (R.I.P.)


Sun Mar 15, 2009 8:09 pm
Profile ICQ YIM
Ambassador
User avatar

Joined: Mon Feb 09, 2004 3:00 am
Posts: 3141
Location: Kansas
Unread post 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


_________________
               / Promethius / Enigma / Wolfen /

"A man who has no skills can be taught, a man who has no honor has nothing."


Sun Mar 15, 2009 8:42 pm
Profile ICQ
Commander
User avatar

Joined: Fri Jun 09, 2006 2:00 am
Posts: 1400
Location: Canada
Unread post 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 & "**"

_________________
----------------------------
-= QUANTUM Computing 101: 15 = 3 x 5 ... 48% of the time.
-= There are 10 types of people in the world: Those that understand Binary and those who do not
-= If Oil is made from Dinosaurs, and Plastic is made from Oil... are plastic Dinosaurs made from real Dinosaurs?
-= I like to keep my friends and my enemies rich, and wait to see which is which - Tony Stark (R.I.P.)


Sun Mar 15, 2009 9:59 pm
Profile ICQ YIM
Commander

Joined: Sun Feb 25, 2001 3:00 am
Posts: 1838
Location: Guam USA
Unread post 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 :)

_________________
TWGS V2 Vids World on Guam Port 2002
Telnet://vkworld.ddns.net:2002
Discord @ DiverDave#8374
Vid's World Discord

Founding Member -=[Team Kraaken]=- Ka Pla

Image
Winners of Gridwars 2010
MBN Fall Tournament 2011 winners Team Kraaken
Undisputed Champions of 2019 HHT Just for showing up!

The Oldist , Longist Running , Orginal Registered Owner of a TWGS server :
Vids World On Guam


Mon Mar 16, 2009 6:57 am
Profile WWW
Gameop
User avatar

Joined: Mon Jan 26, 2009 12:27 am
Posts: 123
Unread post 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 ?

_________________
Dodger known as Yop Solo, Sysop
twgsdodgerbbs.ath.cx
http://cyberquebec.ca/_layout/?uri=http://cyberquebec.ca/twgsdodgerbbs/


Mon Mar 16, 2009 9:43 pm
Profile
Gameop
User avatar

Joined: Tue Nov 19, 2002 3:00 am
Posts: 1050
Location: USA
Unread post 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.

_________________
Dark Dominion TWGS
Telnet://twgs.darkworlds.org:23
ICQ#31380757, -=English 101 pwns me=-
"This one claims to have been playing since 1993 and didn't know upgrading a port would raise his alignment."


Mon Mar 16, 2009 11:57 pm
Profile ICQ
Ambassador
User avatar

Joined: Mon Feb 09, 2004 3:00 am
Posts: 3141
Location: Kansas
Unread post 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.

_________________
               / Promethius / Enigma / Wolfen /

"A man who has no skills can be taught, a man who has no honor has nothing."


Tue Mar 17, 2009 12:06 am
Profile ICQ
Commander

Joined: Sun Feb 25, 2001 3:00 am
Posts: 1838
Location: Guam USA
Unread post 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.

_________________
TWGS V2 Vids World on Guam Port 2002
Telnet://vkworld.ddns.net:2002
Discord @ DiverDave#8374
Vid's World Discord

Founding Member -=[Team Kraaken]=- Ka Pla

Image
Winners of Gridwars 2010
MBN Fall Tournament 2011 winners Team Kraaken
Undisputed Champions of 2019 HHT Just for showing up!

The Oldist , Longist Running , Orginal Registered Owner of a TWGS server :
Vids World On Guam


Tue Mar 17, 2009 1:18 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 23 posts ]  Go to page 1, 2  Next

Who is online

Users browsing this forum: No registered users and 64 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by wSTSoftware.