J.P. Morgan Interview Question

given a function that generates a random integer between 1:5 , how can we generate a random integer 1:7

Interview Answers

Anonymous

Feb 6, 2019

run rand(1:5) twice and record the ordered pair . That makes 25 outcomes. Impose an ordering on these 25 pairs. Now, if the outcome belongs in the first 4 pairs, discard them and the procedure starts over. If not, then note that the remaining 21 pairs can be divided into 7 equally probable subsets.

1

Anonymous

Aug 30, 2018

rand(1:5)*rand(1:1.4) -- gives a float type

Anonymous

Aug 28, 2018

Generate X from 1:5, then (X-1)*6/4+1

1