Massassi Forums Logo

This is the static archive of the Massassi Forums. The forums are closed indefinitely. Thanks for all the memories!

You can also download Super Old Archived Message Boards from when Massassi first started.

"View" counts are as of the day the forums were archived, and will no longer increase.

ForumsCog Forum → More randomn numbers
More randomn numbers
2005-12-17, 7:28 AM #1
Ok finally got around working on my level again. But I need to improve my cog because it has some flaws which can detoriate gameplay.

There are 12 random integers being generated using Rand() and truncating.
It is now however imperative that all the generated numbers are NOT equal to one another. So what I need is somekind of check to see if a newly created number isn't already in use.

Code:
  for ( rndint = 0; rndint < numdoors; rndint = rndint + 1 ){ //repeat 0-11
    rnd = Rand() * 30; //random number between 0 and 30
    rndt = rnd - ( rnd % 1 ); //truncate random value using modulo

   //Here must come some code to test if it isn't used already

    PlaySoundGlobal(open, 1, 0, 0x100);

    if ( rndt == 30 ) { //in the special situation of rand() giving 1
      rndint = numdoors;

      for ( dint = 0; dint < 30; dint = dint + 1 ){ // for 0-29

        if ( GetCurFrame( door[dint] ) == 1 ){ //if open
          MoveToFrame( door[dint], 0, dspeed ); //close
        } //end if open

        else if ( GetCurFrame( door[dint] ) == 0 ){ //if closed
          MoveToFrame( door[dint], 1, dspeed ); //open
        } //end if closed
      } //end for 0-29
    } //end if special situation

    else if ( GetCurFrame( door[rndt] ) == 1 ){ //if open
      MoveToFrame( door[rndt], 0, dspeed ); //close
    } //end if open

    else if ( GetCurFrame( door[rndt] ) == 0 ){ //if closed
      MoveToFrame( door[rndt], 1, dspeed ); //open
    } //end if closed
  } //end for 0-11


Part of my code. numdoors is currently set to 12, so 12 random numbers are generated in this for loop. For each generated number a door is either closed or openend, depending on its current state. In the once in a ~10,000 chance of 30 being returned with generating random numbers ALL doors are changed position.
The problem is a door might be called multiple times and have to open and then immediately close again, which I want to avoid.

curently rndt (the truncated randomvalue) is overwritten each loopcycle, so I figure I'll have to be using rndt[rndint] and assign 11 more rndt-symbols.

But I'm confused as to what code I can use to check if the current rndt isn't already used or not and if so generate a new one and check again, just as long until it's unique.

Any help would be appreciated.
APT 1, 2, 3/4, 5/6
TDT
DMDMD

http://veddertheshredder.com
2005-12-17, 8:21 AM #2
Hmm I was just thinking, would it perhaps be better to use a while loop?

while ( numrnds <= numdoors ) {
}

where numrnds is the number of valid rndt's generated

edit: hmm that gives me some array problematics :S
APT 1, 2, 3/4, 5/6
TDT
DMDMD

http://veddertheshredder.com
2005-12-17, 8:41 AM #3
I would think you could do something like this:

Code:
// Clear Random Array each time.
for (i = 0; i <= 11; i = i + 1) RandArray = -1;
index = 0;
for ( rndint = 0; rndint < numdoors; rndint = rndint + 1 )
{
    //repeat 0-11
tryagain:
    rnd = Rand() * 30; //random number between 0 and 30
    rndt = rnd - ( rnd % 1 ); //truncate random value using modulo
    hit = 0;    
    for (i = 0; i <= 11; i = i + 1) 
    {
       if(rndt == RandArray) hit = 1;
    }
    if(hit)
    { 
       // Random Number already in array
       call tryagain;            
       Return;
    }  
    else
    {
       RandArray[index] = rndt;
       index = index + 1;
    }

    PlaySoundGlobal(open, 1, 0, 0x100);

    if ( rndt == 30 )
    { //in the special situation of rand() giving 1
      rndint = numdoors;
      for ( dint = 0; dint < 30; dint = dint + 1 )
      { // for 0-29
        if ( GetCurFrame( door[dint] ) == 1 )
        {
          //if open
          MoveToFrame( door[dint], 0, dspeed ); //close
        } //end if open
        else if ( GetCurFrame( door[dint] ) == 0 )
        { //if closed
          MoveToFrame( door[dint], 1, dspeed ); //open
        } //end if closed
      } //end for 0-29
    } //end if special situation
    else if ( GetCurFrame( door[rndt] ) == 1 )
    { //if open
      MoveToFrame( door[rndt], 0, dspeed ); //close
    } //end if open
    else if ( GetCurFrame( door[rndt] ) == 0 )
    { //if closed
      MoveToFrame( door[rndt], 1, dspeed ); //open
    } //end if closed
} //end for 0-11



Unfortunately Im not at a computer with JK installed. I cant test.
Admin of Narshaddaa.net's dedicated server. For more information on about the dedicated server: http://jkdf2.net/
2005-12-17, 8:55 AM #4
Code:
  rndint = 0; //initiate

  while ( rndint < numdoors ){ //don't stop till you have 12 values
    rnd = Rand() * 30; //random number between 0 and 30
    rndt[rndint] = rnd - ( rnd % 1 ); //truncate random value using modulo
    for ( dupcheck = 0; dupcheck < rndint; dupcheck = dupcheck + 1 ){ // compare with previous rndts
      if ( rndt[rndint] == rndt[dupcheck] ){ //if equal
        rndint = rndint - 1; //go back one phase
      }      
    }
    rndint = rndint + 1; //next value (if substracted before the last value will be overwritten)
  }
  
  for ( rndint = 0; rndint < numdoors; rndint = rndint + 1 ){ //repeat 0-11
    PlaySoundGlobal(open, 1, 0, 0x100); //doors open
    if ( rndt[rndint] == 30 ) { //in the special situation of rand() giving 1
      rndint = numdoors; //stop for loop
      for ( dint = 0; dint < 30; dint = dint + 1 ){ // for 0-29
        if ( GetCurFrame( door[dint] ) == 1 ){ //if open
          MoveToFrame( door[dint], 0, dspeed ); //close
        } //end if open
        else if ( GetCurFrame( door[dint] ) == 0 ){ //if closed
          MoveToFrame( door[dint], 1, dspeed ); //open
        } //end if closed
      } //end for 0-29
    } //end if special situation
    else if ( GetCurFrame( door[rndt[rndint]] ) == 1 ){ //if open
      MoveToFrame( door[rndt[rndint]], 0, dspeed ); //close
    } //end if open
    else if ( GetCurFrame( door[rndt[rndint]] ) == 0 ){ //if closed
      MoveToFrame( door[rndt[rndint]], 1, dspeed ); //open
    } //end if closed
  } //end for 0-11
  Return;  


meanwhile this is what I came up with myself. According to my own intuition this should work. [text deleted]

and it looks like it does after one simple misplaced character.
APT 1, 2, 3/4, 5/6
TDT
DMDMD

http://veddertheshredder.com

↑ Up to the top!