Randomize & Rnd not generating Random string.

jmason

New Member
Joined
May 2, 2014
Messages
17
I will start by admitting that I haven't actually done the maths on the probability of a repeat but this just seems wrong.

I needed a randomly generated sequence of chars and used the following code as found on these forums with minor additions to change the range of chars that might be selected.

Code:
Public Function RandomizeF(Num1 As Integer, Num2 As Integer)
Dim Rand As String, RandTemp As String
Application.Volatile
getLen = Int((Num2 + 1 - Num1) * Rnd + Num1)
Do
i = i + 1
Do
Randomize
RandTemp = Int((89) * Rnd + 33)
Loop Until (RandTemp = 33) Or (RandTemp >= 35 And RandTemp <= 38) Or (RandTemp >= 47 And RandTemp <= 57) Or (RandTemp >= 64 And RandTemp <= 90) Or (RandTemp >= 97)
Rand = Rand & Chr(RandTemp)
Loop Until i = getLen
RandomizeF = Rand
End Function

Given the possible permutations available here I would have thought the possibilities should be massive however I am getting many repeated values even on a repetitively small sample.

To try it out enter the following in cell A1
Code:
=RandomizeF(8,10)
and then Fill down to cell A100. You will see that you get numerous repeats easier with conditional formatting.

So my question, please, is why and how can I avoid it as really I could do with unique values?

Thanks in advance.

John
 
it actually produces unique values a lot for me, whenever I refresh the calc it usually looks like this... I do a countif for each value and then get a count of the countifs, and it shows i get unique values a lot
6PCYfWR.png
 
Upvote 0
try removing Randomize

Ok Well that is a bit odd. That gives me no repeats. I thought the whole point of putting in the Randomize was to ensure a seeded random to give really random random numbers.

Thanks for your help.

Although I don't understand why it works.

Cheers,

John.
 
Upvote 0
Thanks for the reply.

Given the other reply and the result of removing the Randomize command can I just confirm that you still had it in your code?

As it seem a little odd that it works for you but needs it removed to work for me.

Cheers,

John.
 
Upvote 0
Does this do what you want?

Code:
Public Function RandomizeF(Num1 As Integer, Num2 As Integer)
Dim Rand As String, RandTemp As String, i As Long
Application.Volatile
Randomize
getlen = Int((Num2 + 1 - Num1) * Rnd + Num1)
For i = 1 To getlen
    Do
        RandTemp = Int((89) * Rnd + 33)
        Select Case RandTemp: Case 33, 35 To 38, 47 To 57, 64 To 90, 97: Rand = Rand & Chr(RandTemp): Exit Do: End Select
    Loop
Next
RandomizeF = Rand
End Function
 
Upvote 0
The problem will probably still be there. The link posted by MARK858 shows you how to implement it if needed.
 
Upvote 0

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