Advertisement
debetimi

RandomNumQuestion.txt

Aug 13th, 2013
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. I Got this Crazy Question on PHONE INTERVIEW AT GOOGLE:
  2.  
  3. Design and implement a class to generate random numbers in an arbitrary probability distribution given by an array of integer weights, i.e. for int[] w return a number, n, from 0 to w.length - 1 with probability w[n] / sum(w). Using an existing random number generator with a uniform distribution is permitted.
  4.  
  5. Example distribution:
  6. w = 1, 2, 3, 2, 1
  7.  
  8. Example probabilities:
  9. w / sum = 1/9, 2/9, 1/3, 2/9, 1/9
  10.  
  11. Example results:
  12. n = 0, 1, 2, 3, 4
  13.  
  14. Documentation:
  15.  
  16. Class java.util.Random
  17.  
  18. public int nextInt(int n)
  19.  
  20. Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. The general contract of nextInt is that one int value in the specified range is pseudorandomly generated and returned. All n possible int values are produced with (approximately) equal probability.
  21.  
  22. Parameters:
  23. n - the bound on the random number to be returned. Must be positive.
  24. Returns:
  25. the next pseudorandom, uniformly distributed int value between 0 (inclusive) and n (exclusive) from this random number generator's sequence
  26. Throws:
  27. IllegalArgumentException - if n is not positive
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement