## Facebook Interview Question Software Engineer / Developers

• -3

Given a number x, less than 100. How will you generate true with probability x/100. So if x = 65, how will you generate true with probability 65/100. You can represent true by 1 and false by 0.

Country: -

Comment hidden because of low score. Click to expand.
14
of 18 vote

the easiest way i feel...
int myrand(int x)
{
int y=rand()%100;
if(y<x)
return 1;
return 0;
}

Comment hidden because of low score. Click to expand.
0

Incorrect.

As stated on xhxtxtxpx:x/x/xwxwxwx.xcxpxlxuxsxpxlxuxsx.xcxoxmx/xreference/cstdlib/RAND_MAX/ (remove the x's)

"This value is library-dependent, but is guaranteed to be at least 32767 on any standard library implementation."

If your library uses 32767, then each number from 0 to 67 has a 328/32768 chance of being set to your y, and everything else from 68 to 99 only has a 327/32768 chance.

Comment hidden because of low score. Click to expand.
0
of 2 vote

``````/*
Given a number x, less than 100. How will you generate true with probability x/100. So if x = 65, how will you generate true with probability 65/100. You can represent true by 1 and false by 0.
Developed BY:Suman Roy
Emil : email.suman.roy@gmail.com
*/

#include<iostream>
#include<stdlib.h>
#include<ctime>
using namespace std;
int main(){
int x, temp;
std::cout<<"Enter x\n";
std::cin>>x;
int count1=0;
int count2=0;
for ( int i=0;i<100;i++){
if ( x>100 ) {
std::cout<<"X is greater than 100 \n";
exit(EXIT_SUCCESS);
}
temp=rand() % 100 + 1;
if ( temp <= x ){
count1++;
std::cout<<"true="<<1<<std::endl;
}
else{
count2++;
std::cout<<"False="<<0<<std::endl;
}
}
std::cout<<"true occured="<<count1 <<"\n"<<"False occured="<<count2<<std::endl;
return 0;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

developerslife.ru/3405

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````#include<iostream>
#include<cmath>
#include<cstdlib>
#include<time.h>
using namespace std;
int main(void)
{
srand(time(NULL));
double prob = ((double)rand())/RAND_MAX;	//preserving the assumed uniform-random-nature of output
int input;
cin >> input;
if (prob < input*0.01)	cout << "True" << endl;
else		cout << "Flase" <<  endl;
return 0;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````public class Probability {

public static void main(String[] args) {
int x = 5;
// write a function to generate true with a probability of 65/100
for (int i = 0; i < 10; i++) {
System.out.println(generateTrue(x));
}

}

public static boolean generateTrue(int x) {
double random = Math.random() * 100;
if (random < x) {
return true;
} else {
return false;
}
}``````

}

Comment hidden because of low score. Click to expand.
0
of 0 vote

Fisher Yates Shuffle algo can be used using an array of size 100 ,defining the probability that every x end up in a position above x after shuffling(randomizing).

Comment hidden because of low score. Click to expand.
-1
of 1 vote

In java you can do it as so:

``````public static int getRandom(Random r, int x)
{
return ((r.nextInt(100)) < x) ? 0 : 1;
}``````

There are 0-64 (65) chances that the generator will end up returning 1, and 65-99 (35) chances it will return 0.

Comment hidden because of low score. Click to expand.
-1
of 1 vote

Take an array,'a', of size 100 and fill it with 0's and 1's at random. Take care that number of 1's is exactly 'x'. Now randomly pick a number between 1 and 100,say 'i'. Output a[i].

Drawback: Every time the value of 'x' changes array has to be modified.

Comment hidden because of low score. Click to expand.
-2
of 2 vote

JS:

``return Math.ceil(Math.random()*100) <= x ? true : false``

Comment hidden because of low score. Click to expand.
0

seems like

``return Math.floor(Math.random()*100) < x``

would be better

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book walking you through getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.