VBA Learning notes random number & array ReDim

Source: Internet
Author: User

Discover more simple:

A=application.randbetween (-ten)

Direct generation of random integers from 10 to 10

About two-dimensional arrays ReDim Preserve:

If you use the Preserve keyword, you can only redefine the size of the last dimension of the array, and you cannot change the number of dimensions at all.

Specific reference

1If you use thePreservekeyword, you can only redefine the size of the last dimension of the array, and you cannot change the number of dimensions at all. For example, if the array is one-dimensional, you can redefine the size of the dimension because it is the last dimension and the only one. However, if the array is a two-dimensional or more dimension, only the last dimension will be changed to retain the contents of the array at the same time. The following example shows how to increase the last dimension size for an existing dynamic array without erasing any of the data contained therein. 2 3 ReDimXTen,Ten,Ten)4 . . .5 ReDim PreserveXTen,Ten, the)6 7In the same way, when usingPreserve, the size of the array can only be changed by changing the upper bounds, and changing the nether will result in an error. 8 9 If you change the array to small, the data in the deleted element is lost. If you pass an array to a procedure by address, do not redefine the size of the array's dimensions within the procedure. Ten  OneWhen the variable is initialized, the numeric variable is initialized to0, the variable length string is initialized to a 0-length string (""), while fixed-length strings are used0Fill. The variant variable is initialized to Empty. Each element of a variable of a user-defined type is initialized as a separate variable. Before you use a variable that references an object, you must use theSetStatement to assign an existing object to the variable. The declared object variable has a specific value before it is assigned Nothing, this value indicates that the variable does not point to an instance of any object.  A  -Be careful ifReDimStatement uses a variable that does not exist at the module level or at the procedure level, the statement is equivalent to a declaration statement. If a variable with the same name is created later in a broader scope, even if theOption Explicit,ReDimThis variable will also be used after the declaration, and will not cause a compilation error. In order to avoid such conflicts, we should not putReDimused as a declaration statement, and only as statements that redefine the size of an array.  -  theNote To change the size of the array that the variant contains, you must explicitly declare the variant variable before attempting to change its array size.

The following example would like to use ReDim Preserve, and later realized that it is not used, as a way to learn the next two-dimensional array of ReDim (/-\)

' Generate random numbers
Sub Test () Dim A (10, 2) ' originally wanted to write dim a (), and then for Loop ReDim Preserve A (i,2), found not quite right, a (i,1) what to do? For i = 1 to 10upperbound = 1lowerbound = A (i, 1) = Int ((upperbound-lowerbound + 1) * Rnd + lowerbound) ' @1 A (i, 2) = Application.randbetween (1, +) ' @2 cells (i, 1) = A (i, 1) Cells (i, 4) = A (i, 2) NextEnd Sub ' can see @1 and @ 2 The code works exactly the same

Following from: http://blog.csdn.net/iamlaosong/article/details/38434179

Syntax: rnd[(number)]
If the value of number is Randomize generated
Less than 0, each time using number as the seed of random numbers to get the same result.
Greater than 0, a random number above produces the next random number for the seed.
equals 0, producing the same random number as the most recently generated random number.
Omitted, above a random number for the seed to produce the next random number (with greater than 0).

Description
The RND function returns a value that is less than 1 but greater than or equal to 0.
The value of number determines how the Rnd generates random numbers.
The same sequence is generated for the seed given initially, because each call to the RND function uses the previous number in the sequence as the seed for the next number.
Before calling Rnd, use the parameterless Randomize statement to initialize the random number generator (in the case of parameters, a random number of a particular sequence corresponding to the parameter), which has a seed from the system timer. If you do not use the Randomize statement, the sequence of random numbers produced each time you execute the program is the same.

When the Rnd is followed by a negative number, the same parameter can get exactly the same two sequences, for example, after rnd-1 executes the 1000 random number with Rnd, then executes rnd-1, and then uses Rnd to take 1000 random numbers, 1000 random numbers are exactly the same as the preceding 1000. To get a different sequence, you can use a different negative number, or you can execute randomize number after rnd-1. Note that to get the same sequence, the number two times after randomize must be the same. One of the uses of this method is for encryption and decryption.

To generate a random integer in a range, you use the following formula:
Int ((upperbound-lowerbound + 1) * Rnd + lowerbound)
Here, Upperbound is the upper limit of the range of random numbers, while lowerbound is the lower limit of the range of random numbers.
  Note: If you want to get a repeating sequence of random numbers, call the Rnd with a negative parameter value directly before using a Randomize with a numeric parameter. Using a Randomize with the same number value will not get a repeating sequence of random numbers.

' Generates a non-repeating random number between 20 1-100 public  Sub RndNumberNoRepeat1 ()            Dim Rndnumber, temp (k), I, J, Maxrec as Integer            Randomize (Timer)           ' Initialize random number generator      Maxrec = +                  ' Start output random number from A21      k = 0 Do while      k <          rndnumber = Int (M Axrec * Rnd) + 1          temp (k) = Rndnumber          Cells (k + 1) = Rndnumber for          i = 0 to k-1              If temp (i) = Rndnu Mber then Exit for          Next i          If i = k then k = i + 1          ' MsgBox "random number:" & Rndnumber      Loop      End Sub  

  

VBA Learning notes random number & array ReDim

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.