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

Picking random variable in an array once only

Status
Not open for further replies.
Level 45
Joined
Feb 27, 2007
Messages
5,578
If you want to be able to use this data again without overwriting it then first clone it into a new array and work with that array; if you don't care about losing the data you can just work with x directly.
  1. Randomize a number R from 1-N. N is initially 5 (the total available to be randomly picked), but will be reduced later
  2. Do whatever you want with x[R]
  3. Replace x[R] with x[N] (swap it into its position)
  4. Reduce N by 1
  5. Repeat
If you just want to keep using that same array instead of cloning you can also use the [InitialN + 1]th index (or the [0]th index) to temporarily store x[R] before overwriting it:
  • x[R] -> x[InitialN + 1]
  • x[N] -> x[R]
  • x[InitialN + 1] -> x[N]
 
Status
Not open for further replies.
Top