IBM Interview Question
Software Engineer / Developers#include<stdio.h>
main()
{
int myrand5();
printf("Rand %d\n",myrand5());
}
int myrand5()
{
int a;
return (-1*((int)(&a)%5)+1);
}
using this code you can find random no. from 1-5
----------------------------------------------------------------------------------
#include<stdio.h>
main()
{
int myrand5();
printf("Rand %d\n",(myrand5()+2)%7+1);
}
int myrand5()
{
int a;
return (-1*((int)(&a)%5)+1);
}
this will give you random no.s 1-7
<pre lang="java" line="1" title="CodeMonkey10628" class="run-this">int rand15()
{
return (rand() % 5) + 1;
}
int rand17()
{
return ((rand15() + 7 + rand15()) % 7) + 1;
}
</pre><pre title="CodeMonkey10628" input="yes">
</pre>
Well this answer is partially true. While it is correct that it will generate numbers between 1 and 7, the distribution of these numbers is not going be uniform.
If we try all possible combinations, the frequency of each number will be:
freq[1]=4
freq[2]=3
freq[3]=3
freq[4]=3
freq[5]=3
freq[6]=4
freq[7]=5
While 3 & 4 are expected, 5 is not expected. I think a better choice would be to replace
return ((rand15() + 7 + rand15()) % 7) + 1;
With
return (((rand15() - 1) * 5 + rand15() - 1) * 7) / 25;
The explanation of this is as follows:
It is possible that we will end up with 25 combinations, which we would like to map to 7 numbers. So we map to combinations to numbers linearly. In order to avoid getting 0 all the time, we multiply first, and then divide with the number of combinations. The distirbution becomes as follows:
freq[1]=4
freq[2]=4
freq[3]=3
freq[4]=4
freq[5]=3
freq[6]=4
freq[7]=3
Truly uniform. If 5 and 7 were not pairwise prime, the distribution would have been exactly equal for all numbers.
why not below one
rand7(){
return rand5()*7/5;
}
reasoning
for rand5()=0 rand7() will be 0 too.
for rand5()=5 rand7() will be 7
rest of rand5() ; rand7() should be distributed equally.
x1=rand5();
x2=rand5();
x3 = (x1*10+x2)
finalRand = x3%7 + 1.
frequencies for each digit between 1 and 7 :: [4,3,3,4,4,3,4]
//This would be more uniform
x1=rand5()
x2=rand5()
...
x7=rand5()
sum = x1+...x7
finalRand = sum%7 + 1
simple but not good. why? because the numbers generated using this algo are not equally distributed, it is more as a gaussian curve.
how? assume 'randHi' generates between 0 and 2, then as per ur algorithm all the possible numbers are 1+0,2+0,3+0,4+0,5+0,1+1,2+1,3+1,4+1,5+1,1+2,2+2,3+2,4+2,5+2 --> [1,2,3,4,5,2,3,4,5,6,3,4,5,6,7]
frequencies of numbers between 1 to 7 --> [1,2,3,3,3,2,1]
probabilities of numbers between 1 to 7 -->[1/15,2/15,3/15,3/15,3/15,2/15,1/15]
as you can see this is a bell curve i.e. the probability of generating 1 or 7 is lower than generating 2 or 6. which is lower than generating 3 or 4 or 5.
we want our function to generate all numbers between 1 to 7 with equal probabilities. i.e. 1/k
- write a function Rand12 which return 1 or 2 with equal prob (50/50 chance) using the 1 to 5 generator you have. Just ignore any 3, 4, or 5 until you get 1 or 2.
- draw a number 1 - 5, call it X, then if Rand12 is 2, add 5 to X. Now X is uniform 1 - 10. Keep doing this step until your 1 - 10 number is not 8, 9, or 10.
Not too efficient but it works.
Lets represent 7 in base 5. It will require 2 bits. So, call Rand5() for selecting the zeroth bit and rand5() for calling 1st bit. If the result is greater than 7, discard it. Note that the first bit doesn't depend on the second bit. So, probably of each bit is random, hence the final number is also random. I think this approach works for all these sort of examples.
Please correct me if I am wrong.
It is impossible for me not to understand why won't this be a good solution:
rand15()-1 -random btw. 0 and 4
(rand15()-1)/2.0 -random btw. 0 and 2
((rand15()-1)/2.0)*3 -random btw. 0 and 6
1+((rand15()-1)/2.0)*3 -random btw. 1 and 7
1. rand5() will give result from 0 to 5.
2. Probablity of getting an even number or an odd number (assuming rand5() is perfect) will be .5 each
3. 7 when converted into binary has 3 set bits.
4. call rand5() 3 times if result is even then set the corresponding bit otherwise make that bit 0.
Basically here I am trying to generate all the bits with equal probablity
psuedo code:
int multiplicationFactor = 1;
int rand7Number = 0;
for(int i = 0 ; i < 3 ; i++)
{
int j = rand5();
rand7Number += (j%2) * multiplicationFactor ;
multiplicationFactor *= 2;
}
return rand7Number ;
for rand7, each number between 1 and 7 has roughly 15% probability to be selected.
for rand5, each number between 1 and 5 has 20% probability to be selected.
What about we use rand5() to control the probabilities for 1-7 to be selected?
Like for rand5, the probability of a number is not being selected is 80%.
code:
int a = rand5(), b = rand5();
if (a!=1 && b != 1) return 1;
else if (a!=1 && b != 2) return 2;
else if (a!=1 && b != 3) return 3;
else if (a!=1 && b != 4) return 4;
else if (a!=1 && b != 5) return 5;
else if (a!=2 && b != 1) return 6;
else if (a!=3 && b != 1) return 7;
Hello,
- upendra December 24, 2010does that mean for each execution it should give a different answer?