https://github.com/mission-peace/interview/blob/master/src/com/interview/random/RandomCountrySelectionByPopluation.java
public int getRandom(int []arr){
int sum[] = new int[arr.length];
sum[0] = arr[0];
int n = arr[0];
for(int i=1; i < sum.length; i++){
sum[i] = sum[i-1] + arr[i];
n += arr[i];
}
int ran = (int)(Math.random()*n + 1);
int low = 0;
int high = arr.length-1;
int mid = (high + low)/2;
while(true){
if(sum[mid] >= ran && (mid-1 == -1 || sum[mid-1] < ran)){
break;
}
if(sum[mid] > ran){
high = mid-1;
}else{
low = mid+1;
}
mid = (high + low)/2;
}
return mid;
}
RandomCountrySelectionByPopluation rcsp = new RandomCountrySelectionByPopluation();
int arr[] = {5,5,2,3,7,1};
for(int i=0; i < 10; i++){
System.out.print(rcsp.getRandom(arr));
}
https://github.com/mission-peace/interview/blob/master/src/com/interview/random/SelectMRandomNumbersInStream.java
public int[] selectRandom(int arr[],int m){
int result[] = new int[m];
for(int i=0; i < m ;i++){
result[i] = arr[i];
}
for(int i=m ; i < arr.length; i++){
int random = (int)(Math.random()*i) + 1;
if(random <= m){
result[random-1] = arr[i];
}
}
return result;
}
int rand7 () {
while (true) {
int num = 5 * randS() + randS();
if (num < 21) {
return num % 7;
}
}
}
int rand7() {
while (true) {
int rl = 2* rands(); /* evens between 0 and 9 */
int r2 = rand5(); /*used later to generate a·0 or 1 */
if (r2 != 4) {
/* r2 has extra even num-discard the extra* /
int randl = r2 % 2; /*Ge nerate 0 or 1 */
int num = rl + randl; /* will be in the range 0 to 9 */
if (num < 7) {
return num;
}
}
}
public int getRandom(int []arr){
int sum[] = new int[arr.length];
sum[0] = arr[0];
int n = arr[0];
for(int i=1; i < sum.length; i++){
sum[i] = sum[i-1] + arr[i];
n += arr[i];
}
int ran = (int)(Math.random()*n + 1);
int low = 0;
int high = arr.length-1;
int mid = (high + low)/2;
while(true){
if(sum[mid] >= ran && (mid-1 == -1 || sum[mid-1] < ran)){
break;
}
if(sum[mid] > ran){
high = mid-1;
}else{
low = mid+1;
}
mid = (high + low)/2;
}
return mid;
}
RandomCountrySelectionByPopluation rcsp = new RandomCountrySelectionByPopluation();
int arr[] = {5,5,2,3,7,1};
for(int i=0; i < 10; i++){
System.out.print(rcsp.getRandom(arr));
}
https://github.com/mission-peace/interview/blob/master/src/com/interview/random/SelectMRandomNumbersInStream.java
public int[] selectRandom(int arr[],int m){
int result[] = new int[m];
for(int i=0; i < m ;i++){
result[i] = arr[i];
}
for(int i=m ; i < arr.length; i++){
int random = (int)(Math.random()*i) + 1;
if(random <= m){
result[random-1] = arr[i];
}
}
return result;
}
public void shuffle(int arr[]){
for(int i=arr.length-1; i>=0; i--){
int random = (int)(Math.random()*(i+1)) ;
System.out.print(random + " ");
swap(arr,random,i);
}
}
https://github.com/mission-peace/interview/blob/master/src/com/interview/random/Rand7UsingRand5.java
public int rand7(){
int r = (rand5()-1)*5 + rand5();
while(r > 21){ // I just need to ignore [22, 25]
r = (rand5()-1)*5 + rand5();
}
return (r%7) + 1;
}
private int rand5(){
return (int)(Math.ceil(Math.random()*5));
}
int rand7 () {
while (true) {
int num = 5 * randS() + randS();
if (num < 21) {
return num % 7;
}
}
}
int rand7() {
while (true) {
int rl = 2* rands(); /* evens between 0 and 9 */
int r2 = rand5(); /*used later to generate a·0 or 1 */
if (r2 != 4) {
/* r2 has extra even num-discard the extra* /
int randl = r2 % 2; /*Ge nerate 0 or 1 */
int num = rl + randl; /* will be in the range 0 to 9 */
if (num < 7) {
return num;
}
}
}