Algorithm exercise: generate random numbers within a specified range

Source: Internet
Author: User

Algorithm exercise: generate random numbers within a specified range
I. Problem Description

Given an integer array and a range of [min, max], write a function to generate a random number within the range, and the random number is not in the array, assuming that the random number can always be found.

 

Ii. Analysis and Solutions

You can answer the question in two steps. Step 1: generate a random number x included in [min, max]. Step 2: Determine whether the random number is in the array. If yes, regenerate a random number and then execute step 2, otherwise, the random number is returned.

When determining whether a random number is in an array, if sequential search is used, the time complexity of the search is O (n ). In the worst case, assume that the first n random numbers are all in the array until n + 1 meets the conditions, the time complexity of the entire algorithm is O (n ^ 2 ).

So, can we further improve the efficiency? After analysis, we can sort the array in the weighted part, and then use the binary search method, make the search time complexity from O (n) ---> O (logn ). Therefore, the time complexity of the entire algorithm is O (nlogn ).

 

Iii. Code and implementation

 

# Include
 
  
# Include
  
   
# Include
   
    
Using namespace std; int RandNumInRange (int nArray [], int nCount, int nMin, int nMax) {if (nMin> nMax) {int temp = nMax; nMax = nMin; nMin = temp;} if (nMin = nMax) {return nMin;} int nRet = nMin + rand () % (nMax-nMin ); if (nArray = NULL | nCount <= 0) {return nRet;} // sort (nArray, nArray + nCount) in ascending order; while (true) {int low = 0; int high = nCount-1; int mid = 0; bool bContinue = false; while (l Ow <= high) {mid = (low + high)/2; if (nArray [mid] = nRet) {bContinue = true; break ;} else if (nArray [mid]> nRet) {high = high-1 ;}else {low = low + 1 ;}} if (! BContinue) {break;} // re-generate a random number nRet = nMin + rand () % (nMax-nMin);} return nRet;} int main () {// Random Seed srand (unsigned) time (NULL); int nArray [] = {10, 12, 18, 14, 15, 13 }; for (int I = 0; I <20; ++ I) {cout <RandNumInRange (nArray, _ countof (nArray), 5, 5) <endl ;} return 0 ;}
   
  
 

 

 

 

Series of articles:
1. This series of articles [algorithm exercises] are only a record of my learning process and self-motivation. They do not mean any preaching. It is my pleasure to give readers some knowledge and insights.
2. This series of articles is my understanding of some of the experiences written by Chen Dongfeng's book "coming into silicon valley, programmer interview secrets". Most of the opinions in this article come from this book!
3. There are many mistakes and shortcomings in the article. You are welcome to criticize and correct them. Thank you.

 

 


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.