Introduction to the algorithm calls random (0,1) to implement the random (A, b) process

Source: Internet
Author: User
Tags random seed

An implementation of the procedure describing the random (A, b), which only calls random (0,1). As A and B functions, what is the expected run time of your program? (RANDOM (0,1) with equal probability output 0 or 1,random (A, a, b) output the number of [A, b] between equal probability (integer))

to the probability of random (A, b) output the number between [A, a], as long as the probability of getting [0,b-a] a number between. Since it is possible to get 1 or 0 by random (0,1), it is possible to divide the [0,b-a] interval into smaller intervals, assuming that when the 1 o'clock interval is reduced to [(B-A)/2,b-a],0 It is [0, (B-A)/2]. So recursion, finally get only an integer interval if [x,x], at this time a+x is the final result.

The above idea seems very good, but there is a pretty deadly problem not to consider, that is [0,b-a] in the b-a+1 must be 2^x (power of 2) to ensure equal probability. For a chestnut, [0,2] has 3 numbers {0,1,2} and the probability of getting 2 is significantly greater than 0 and 1. The solution here is that the number of integers in the extended interval guarantee interval is a power of 2. When the resulting integer is larger than b-a, it is re-executed. So

1 //Random.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include"windows.h"6#include"stdio.h"7#include"math.h"8#include"time.h"9 Ten /* One implementing the random (0,1) function A */ -  - intEitherrand () the { -     returnRAND ()%2; - } - /* + perform recursion - */ + LongRandint*arr,intBegintend) A { at     if(Beg = =end) -     { -         returnBeg; -     } -     Else -     { in         returnEitherrand () = =0? rand (Arr,beg, (beg+end)/2): Rand (arr, (beg+end)/2+1, end);  -     } to } + intMain () - {     the     intA=2; *     intb=7; $     intdif=b-a+1;Panax Notoginseng     Longrs =1; -     intExp =0;  the      while(rs<dif) +     { Ars = rs*2; theExp + +; +     } -     int*arr = (int*) malloc (rs*sizeof(Long)); $     //random seed, set it all the time. Duplicate settings produce the same value $Srand ((unsigned) time (0)); -     intRdnum = rand (arr,0, rs-1); -      while(rdnum>dif) the     { -Rdnum = rand (arr,0, rs-1);Wuyi     } theprintf"%d", rdnum+a); -System"Pause"); Wu     return 0; -}

Introduction to the algorithm calls random (0,1) to implement the random (A, b) process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.