top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Write a method to generate a random number between 1 and 7?

+2 votes
394 views
Write a method to generate a random number between 1 and 7?
posted Dec 30, 2013 by Atul Mishra

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Do u need a code or algo??
What is the nature of Randomness??

1 Answer

+1 vote
int i;
do
{
  i = 5 * (rand5() - 1) + rand5();  // i is now uniformly random between 1 and 25
} while(i > 21);
// i is now uniformly random between 1 and 21
return i % 7 + 1;  // result is now uniformly random between 1 and 7
answer Dec 30, 2013 by Sandeep
...