Depending on your needs, you can do it wherever you want. <Array>.random() is a function that returns a random element in the array. So, we use a variable to store it while we use it.
// Example using $pies again
// Here we create an array with 5 elements and store it in _pies as a temporary variable.
// Each one is a string which we know contains a flavor of pie.
// Depending on whether we will use this past this passage we can make it a story variable ($pies) or a temporary variable (_pies) (which goes away after we leave the passage).
<<set $pies = [ "Blueberry", "Cherry", "Cream", "Pecan", "Pumpkin" ]>>
// Now, we need to pick a random flavor of pie.
// We'll need to check what flavor we got several times so we'll also store it in a temporary variable.
// This uses the .random() function on our array $pies to pick a random flavor, then stores it in _randomFlavor
<<set _randomFlavor = $pies.random()
// NOTE: We can also use the SugarCube function either() and it would look like
// <<set _randomFlavor = either($pies)>>
//Then we can use that stored random variable in an if statement to end up with a result.
<<if _randomFlavor == "Blueberry">>
//Something here happens related to blueberry
<<elseif _randomFlavor == "Cream">>
//Something here happens related to cream
//etc.
<</if>>
We use the $pies.random() inside the <<set>> macro so that we can store the result in a variable. Another example is something like the following.
<<set $pies = [ "Blueberry", "Cherry", "Cream", "Pecan", "Pumpkin" ]>>
// If we use a <<print>> macro several times with $pies.random() we'll get a random result every time.
<<print $pies.random()>>
<<print $pies.random()>>
<<print $pies.random()>>
<<print $pies.random()>>
// That might output something like
Pumpkin
Cream
Pecan
Pumpkin
// Every time we use .random() on $pies it selects a new random pie flavor.
You probably want to create and use the variable containing the random pie flavor in the passage that you're using it. If you make your variable in StoryInit, it will be the same every time you call on it. That's because it will have selected a random flavor when the story started up and stored it. For example
If we have this in our StoryInit:
<<set $pies = [ "Blueberry", "Cherry", "Cream", "Pecan", "Pumpkin" ]>>
<<set $randomFlavor = $pies.random()>>
then when we call on it, it will be the same every time.
// Using these print statements, the variable was already created in StoryInit.
<<print $randomFlavor>>
<<print $randomFlavor>>
<<print $randomFlavor>>
<<print $randomFlavor>>
// That would now output something like this
Pecan
Pecan
Pecan
Pecan
// Or if you start the story up again and do the same thing.
Pumpkin
Pumpkin
Pumpkin
Pumpkin
That's because we ran $pies.random() when the story started, so what we got was already stored.