Generate Random ODD numbers

neodjandre

Well-known Member
Joined
Nov 29, 2006
Messages
950
Office Version
  1. 2019
Platform
  1. Windows
Hello,

Can an expert in this forum devise of a VBA macro that creates 500 random numbers in cells A1:A500?

The numbers must all be odd and contain 3 digits in each cell... i.e.

135
179
977
etc...

many thanks!
andy
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
yeah not very neat but I did it ... thanks anyway for your help all
 
Upvote 0
=odd(int(rand()*9))&odd(int(rand()*9))&odd(int(rand()*9))
 
Upvote 0
Something like:

Code:
Sub veryOddRandom()
    oddarray = Array(1, 3, 5, 7, 9)
    l = 3
    For Each cel In Range("A1:C100")
        r = ""
        For i = 1 To 3
            r = r & oddarray(Int(Rnd() * 5))
        Next
        cel.Value = r * 1
    Next
End Sub
 
Upvote 0
yes! ***Brownie Points to both WestMan & Weaver

and special thanks to everyone else of course!
 
Upvote 0
Maybe just me, but I still got some even digits in West Man's.

Overly expensive use of evaluate, but just for kicks...

Rich (BB code):
Sub exa3()
Dim a(1 To 500, 1 To 1)
Dim i As Long
    
    For i = 1 To 500
        a(i, 1) = CLng(Evaluate("=ODD(TRUNC(RAND()*(9-1)+1))") & _
                       Evaluate("=ODD(TRUNC(RAND()*(9-1)+1))") & _
                       Evaluate("=ODD(TRUNC(RAND()*(9-1)+1))"))
    Next
    Range("A1:A500").Value = a
End Sub

Weaver: That's sharp!

Hi Richard :)

Mark
 
Upvote 0
Formula-wise both =odd(int(rand()*9)) and Odd(randbetween(1,9)) will have a bias (the first gives you less 9s and the second gives you less 1s) so for an even distribution you could use three of these concatenated

=INT(RAND()*5+1)*2-1
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top