Randomly pick a Term / Definition to quiz the user on

As noted earlier, we read questions in from a CSV and then we get a count of the questions ($defCount).  

Next we want to randomly pick a question from the imported list.  This is where the Powershell command Get-Random comes into play.  We supply the range of the number that we want to generate.  In this case, we are looking for a random number between 0 and $defCount.  The random number is then placed into the variable $pickQuestionNum.

Finally, we display the definition (the question) of the random selection.

# -----------------------------------------------------------------------
# Read in data and get count
# -----------------------------------------------------------------------
$tester = Import-Csv C:\scripts\Input\test.csv
$defCount = $tester.Count -1

# -----------------------------------------------------------------------
# Pick and display a random definition (also put a blank line after)
# -----------------------------------------------------------------------
$pickQuestionNum = Get-Random -minimum 0 -maximum $defCount
$tester.Item($pickQuestionNum).definition

No comments:

Post a Comment