Generates K distinct integers at random, showing the maximum number.

Source: Internet
Author: User
Tags benchmark

Analyze this question. 1. Generate K unequal integers. 2. sort and find the largest one. 3. display. Specifically, pay attention to the following issues: 1. Generate a random number, define an array, and generate each number in sequence. 2. to solve the non-repetition problem, you must first be bold. Assume that the first number produced by the array is the largest, and then compare it with each number in the following array to obtain the maximum number. During the comparison process, there will be two situations: one is that there are no duplicates, the number is saved, the comparison continues smoothly, the result is obtained, and the other is that there are duplicates, you have to discard this number, generate a new one, save the new non-repeated number, and continue. It is worth mentioning that there will be a benchmark here to tell you how to repeat and what to do without repeat. This is very important.

The Code is as follows:

Dim A () as long

Private sub form_click ()
Dim K as integer, M as integer, N as integer
K = Val (inputbox ("Enter K") 'generates K Integers
M = Val (inputbox ("Please input m") 'integer range, M is the lower bound
N = Val (inputbox ("Enter N") 'n' is the upper bound

Redim A (k-1)
Temp = int (m + (n-m + 1) * RND () 'randomly generates a number and assigns it to the first number of arrays A (0)
C = temp
A (0) = C
Print A (0)

'Number of possible duplicates after processing the second one
For I = 1 to k-1
C = int (m + (n-m + 1) * RND ())
Flag = 1' if you do not repeat the flag here, it is an important benchmark!

For T = 0 to I-1 'Here, the maximum number obtained previously is compared with the number of I generated so far.
If C = a (t) then
I = I-1 'discard this number and generate another number
Flag = 0
Exit
End if
Next t

If flag = 1 then', save it to the array.
A (I) = C
Print C' display on screen
End if

Next I
End sub

 

 

 

 

 

 

 

Related Article

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.