Excel VBA Rnd Not Actually Random

0 votes

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

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

The numbers aren't really random, though, which is the problem. As an illustration, let's imagine that after starting the macro and pressing the button twice, I receive the digits 5 and 2. I get the same two numbers, 5, and 2, whether I close the macro, reopen it, or click the button twice.

It's been years since I used VB.net, so I don't quite remember how to do it. I also wouldn't know how to use it in Excel VBA. However, I do know that there was a technique to truly make it spew out random numbers each time without repeating the "sequence" in VB.net.

Oct 27 in Others by Kithuzzz
• 20,660 points
50 views

1 answer to this question.

0 votes

 Initialize the random number function.

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

I hope this solves your problem.

answered Oct 27 by narikkadan
• 37,660 points

Related Questions In Others

0 votes
1 answer

Language independent way to get "My Documents" folder in VBA Excel 2003

 Hello :)  This code may help you in your ...READ MORE

answered Feb 16 in Others by Edureka
• 13,640 points
87 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Runtime error 438 while importing data in excel from secured website using VBA

Replace With ieDoc.forms(0) .userType.Value = "1" ...READ MORE

answered Sep 23 in Others by narikkadan
• 37,660 points
104 views
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,720 points
452 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
2,219 views
0 votes
1 answer

Using VBA Excel to create a gramatically correct list

The Excel AND function is a logical ...READ MORE

answered Feb 9 in Others by gaurav
• 22,040 points
69 views
0 votes
2 answers

How to copy a formula horizontally within a table using Excel VBA?

Hi so basically, create an adjacent column ...READ MORE

answered Feb 16 in Others by Edureka
• 13,640 points
207 views
0 votes
1 answer

Excel VBA is not recording macro properly

The "awkward code" is an R1C1 notation ...READ MORE

answered Nov 15 in Others by narikkadan
• 37,660 points
26 views
0 votes
1 answer

Excel VBA if file closed, then open and paste, else just paste data

Slightly re-worked to add full workbook/sheet qualifiers ...READ MORE

answered Sep 21 in Others by narikkadan
• 37,660 points
88 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP