Google Interview Question

select a random number between 1 & 7

Interview Answers

Anonymous

Sep 2, 2012

(System.currentTimeMillis() % 7) + 1;

6

Anonymous

Dec 9, 2012

I think they're looking deeper, like how would you generate "randomness" to produce a number between 1 and 7, like a linear congruential generator.

1

Anonymous

May 3, 2016

Three!

1

Anonymous

Sep 10, 2016

Five!

Anonymous

Sep 8, 2012

This problem is ill-defined. What kind of number? I would tell them I select pi if they don't tell you what kind of objects you are working with.

1

Anonymous

Dec 3, 2012

For floats: (new Random()).nextFloat() * 6 + 1; For ints: (new Random().nextInt(6) + 1; Use the 1-argument Random constructor with a random seed for non-deterministic results.