Excel VBA Rnd is not actually random

I currently have a macro that, when I click the button, "randomly" gives me a number in msgbox:

dim number as double number= Int(8 * Rnd + 1) - 1 MsgBox number 

The fact is that the numbers are not actually randomized. For example: if I run a macro, double-click the button, let's say I get the numbers 5 and 2. Now, if I close the macro and open it again and double-click the button, I get the same two numbers 5 and 2.

Now I know that in VB.net there was a way to make him spit out random numbers every time without repeating the "sequence", but these have been the years since I touched vb.net, so I don’t quite remember, also I wouldn’t knew how to use it in excel vba.

+5
source share
1 answer

You need to initialize the random number function

 Sub test() Dim number As Double Randomize number = Int(8 * Rnd + 1) - 1 MsgBox number End Sub 
+16
source

Source: https://habr.com/ru/post/1204338/


All Articles