• 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.

Random Bases

Status
Not open for further replies.
Level 11
Joined
Jun 30, 2008
Messages
580
I am not sure if I am just having a brain fart or something else but:

I am trying to set up random bases in my map. I can't seem to figure out how to randomly pick one of the base regions. Example: If the region is already taken repick a random region and check again, but I can't seem to figure out how to do this?

Thanks.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
Make an array of free regions. Pick a random index from the array. Use the region and remove it (replacing it with the final region from the array). Repeat (pick another index etc).

Remember to keep track in an integer of how many regions still available. Every time one is picked, you need to reduce it by 1. Conveniently, this can be used to get the final region which is used to swap out the used region.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
Below is a more detailed description of a way to get unique random elements (no duplicates) from a set of simlar data.

Make a continious array of your regions and store the number of elements you have in the array in another variable.
regarray[0] = region1
regarray[1] = region1
regarray[2] = region1
regn = 3

get a random number in the order of 0 to regn-1
regrandom = random(0,regn-1)

Now process the random region (doing what you want to it or using it).
process(regarray[regrandom])

Now remove the used index from the array.
regn = regn - 1 //decriment regn
regarray[regrandom] = regarray[regn]

Repeat these steps until you have all the random regions you need, or there are no more random regions to pick.
 
Status
Not open for further replies.
Top