Using Random5 function ( which returns a integer *k* [1,5] with probability ) , design Random7 function ( which returns a integer *k* [1,7] with probability ).

Advertisements

An attempt to pass time wisely

Using Random5 function ( which returns a integer *k* [1,5] with probability ) , design Random7 function ( which returns a integer *k* [1,7] with probability ).

Advertisements

%d bloggers like this:

December 11, 2009 at 7:42 pm |

Solution due to Krishna Chaitanya ( chaitanya518@gmail.com )

Random7 ( ) {

while ( 1 ) {

a = rand5()-1

b = rand5()-1

c = 5*a+b /* c is decimal equivalent of (ab) in BASE-5 notation */

if(c<21)

return c%7 + 1 /* this distributes integers from [0,20] into 7 buckets, 3 in each */

}

}