• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[Solved] Random - No Repeat

Status
Not open for further replies.
yes for this i will make an example of three integers.

here it is [hidden="here is new code]
  • random non repeating number
    • Events
      • Player - Player 1 (Red) skips a cinematic sequence
    • Conditions
    • Actions
      • Set tempInt[1] = (Random integer number between 1 and 2)
      • Set tempInt[2] = randomIntegerArray[tempInt[1]]
      • Game - Display to (All players) the text: (String(tempInt[2]))
      • Set randomIntegerArray[tempInt[1]] = randomIntegerArray[3]
      • Set randomIntegerArray[3] = tempInt[2]
[/hidden]

this will not let the same random integer be shown more than once in a row.

here is a test map just hit esc and it will show the integers.
map is down bottom
 
Last edited:
I just use another array index to store the last selected number. It is moved to the last index and the number that was previously the last index was move to the one b4 that. I did make one mistake tho u should store all integers from index 1 to 3 and only use index 1 and 2. So u need to reduce the 4 u see in the trigger to 3 and the loop from 1 to 3 to 1 to 2 and it will work as intended
 
Level 5
Joined
May 6, 2013
Messages
125
If you really only need 1 number inbetween (so that the numbers 1-2-1 are valid), there is no need for a whole array to store those numbers. just get numbers from a shorter range and map the result to not contain the number you got last time. something like this:
Code:
if someBoolTellingThatWeAllreadyDidThisOnce == false:
  rand = random number between range_start and range_end
else
  rand = random number between range_start and range_end - 1
  if (rand >= lastNumber) rand++
  lastNumber = rand

If you need more random numbers in a row that musnt repeat, i'd probably go with rulerofirons idea. It consumes more memory and is not really dynamic as you have to prepare it for certain ranges, but once prepaired, its O(1). You could try to map them again, but at least using the mapping technique above, it would require you to always sort the array of allready-used numbers, and implimenting sorting algorythms is kind of a hassle in warcraft as there are no pointers.
 
Level 5
Joined
May 6, 2013
Messages
125
im well aware of what you did. the "techinque above" was referring to the pseudo-code i posted, and the primitive mapping technique i used would indeed need a sorted buffer to work.
And still, your version requires the use of a prepared buffer to hold random numbers, much like the version rulerofiron suggested, while only allowing for 1 number to not appear consecutively
 
All he asked for was that the random number to not be displayed twice in a row. Otherwise I would've made it for more.

Edit: i redid the code to fix that small bug.

  • random non repeating number
    • Events
      • Player - Player 1 (Red) skips a cinematic sequence
    • Conditions
    • Actions
      • Set tempInt[1] = (Random integer number between 1 and 2)
      • Set tempInt[2] = randomIntegerArray[tempInt[1]]
      • Game - Display to (All players) the text: (String(tempInt[2]))
      • Set randomIntegerArray[tempInt[1]] = randomIntegerArray[3]
      • Set randomIntegerArray[3] = tempInt[2]
 

Attachments

  • random integer generator.w3x
    12.1 KB · Views: 110
Last edited:
Level 7
Joined
Nov 18, 2012
Messages
272
All he asked for was that the random number to not be displayed twice in a row. Otherwise I would've made it for more.

Edit: i redid the code to fix that small bug.

  • random non repeating number
    • Events
      • Player - Player 1 (Red) skips a cinematic sequence
    • Conditions
    • Actions
      • Set tempInt[1] = (Random integer number between 1 and 2)
      • Set tempInt[2] = randomIntegerArray[tempInt[1]]
      • Game - Display to (All players) the text: (String(tempInt[2]))
      • Set randomIntegerArray[tempInt[1]] = randomIntegerArray[3]
      • Set randomIntegerArray[3] = tempInt[2]
Ok! Ty!
 
I was testing it out but the int stays at 0, Why?

  • CompNames
    • Events
      • Time - Every 1.00 seconds of game time
    • Conditions
    • Actions
      • Set iCountDown = (iCountDown + 1)
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
        • Then - Actions
          • Set iRandom[1] = (Random integer number between 1 and 50)
          • Set iRandom[2] = iRandomIndex[iRandom[1]]
          • Multiboard - Set the text for Multiboard item in column 2, row (iCountDown + 1) to sCompName[iRandom[2]]
          • Game - Display to (All players) the text: (String(iRandom[2]))
          • Set iRandomIndex[iRandom[1]] = iRandomIndex[3]
          • Set iRandomIndex[3] = iRandom[2]
        • Else - Actions
 
Ok I used this trigger:
  • CompNames Copy
    • Events
      • Time - Every 1.00 seconds of game time
    • Conditions
    • Actions
      • Set iCountDown = (iCountDown + 1)
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
        • Then - Actions
          • Set Temp_Int = (Random integer number between 1 and 10)
          • Multiboard - Set the text for Multiboard item in column 2, row (iCountDown + 1) to sCompName[Temp_Int]
          • Game - Display to (All players) the text: (String(Temp_Int))
        • Else - Actions
and this is the results: 9; 3; 6; 10; 8; 5; 10; 4; 1; 4
'10' and '4' are repeated twice and I am very VERY sure that the 'Use Fixed Random Seed' is unchecked.
 
Level 37
Joined
Mar 6, 2006
Messages
9,243
  • Untitled Trigger 003
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Set numberMax = 10
      • For each (Integer A) from 1 to numberMax, do (Actions)
        • Loop - Actions
          • Set numbers[(Integer A)] = (Integer A)
  • Untitled Trigger 001
    • Events
      • Player - Player 1 (Red) skips a cinematic sequence
    • Conditions
    • Actions
      • Set integer = (Random integer number between 1 and numberMax)
      • Set randNumber = numbers[integer]
      • Set numbers[integer] = numbers[numberMax]
      • Set numberMax = (numberMax - 1)
 
Status
Not open for further replies.
Top