Q.
The following algorithm (this is one of the std. algorithms) prints n unique random numbers in the range 0..1000 inclusive
Code:
BEGIN GenerateRandoms(n)
used = boolean array of size 1001
FOR i=0 TO 1000
used(i) = FALSE
NEXT i
FOR i=1 TO n
nextNum = random integer in range 0..1000
WHILE used(nextNum) DO
nextNum = random integer in range 0..1000
ENDWHILE
PRINT nextNum
used(nextNum) = TRUE
NEXT i
END GenerateRandoms
1. Dave calls the routine GenerateRandoms(1337). Identify the error that subsequently occurs, and describe why it occurs. (2 marks)
2. Dave now requires the generation of n unique random numbers in the range 0..10
12. Outline potential issues that would occur if the existing algorithm was modified to accommodate this new range. (2 marks)
3. Dave additionally guarantees that n will always be less than 1000. Propose and give pseudocode for a new algorithm that resolves the issues raised in part (2). (4 marks)
4. You are given access to a library routine that randomly shuffles a list of k items very quickly. Outline how this subroutine could be utilised in an efficient algorithm to solve part (3). (3 marks)
5. Explain how the shuffling subroutine in the library could be implemented. (2 marks)