To create a list of unique random numbers using an array formula
=SMALL(IF(COUNTIF(C$1:C1,ROW($1:$50))<>1,ROW($1:$50)),1+INT(RAND()*(RIGHT(C$1,LEN(C$1)-FIND("-",C$1))-LEFT(C$1,FIND("-",C$1)-1)+1-ROW()+ROW(C$2))))
To enter it correctly, hold CTRL+SHIFT and press ENTER key on your keyboard. If you need to generate random unique numbers from a different range, you have to replace the numbers (1 and 50) in both A1 and A2 cells.
Now you need to copy cell A2 and paste it to as many rows as many random numbers you are expecting to obtain (up to 50 in the example).
To create a list of unique random numbers using UDF (user defined function)
- Press ALT+F11 on your keyboard to open up the VB Editor.
- If no Module is available within your Project, then right-click within the VBA Project Explorer and select Insert, then choose Module from the menu.
- Enter the following code in the module:
'Generates x unique random numbers between any 2 numbers you specify
'e.g. =GetUnique(1,50,8) produces 8 unique random numbers between 1 and 50
Dim iArr As Variant
Dim i As Integer
Dim r As Integer
Dim temp As Integer
Application.Volatile
ReDim iArr(Lo To Hi)
For i = Lo To Hi
iArr(i) = i
Next i
For i = Hi To Lo + 1 Step -1
r = Int(Rnd() * (i - Lo + 1)) + Lo
temp = iArr(r)
iArr(r) = iArr(i)
iArr(i) = temp
Next i
For i = Lo To Lo + Nr - 1
If i = Lo Then GetUnique = GetUnique & "" & iArr(i)
If i > 1 Then GetUnique = GetUnique & " * " & iArr(i)
Next i
GetUnique = Trim(GetUnique)
End Function
- Use the function in any selected cell by entering it in the format shown above on the comment line.
To create a random letter of alphabet (eithe Upper- or Lowercase)
It generates a random letter from a to z and A to Z characters.
To create a string of random letters,
like in this example:
=IF(RAND()<=0.5,CHAR(RANDBETWEEN(65,90)),CHAR(RANDBETWEEN(97,122)))&IF(RAND()<=0.5,CHAR(RANDBETWEEN(65,90)),CHAR(RANDBETWEEN(97,122)))&IF(RAND()<=0.5,CHAR(RANDBETWEEN(65,90)),CHAR(RANDBETWEEN(97,122)))
This specific formula generates a string of three random letters. If you drag the cell fill handle over to some range you want to cover with this formula, you will get the range filled with random 3-letter strings. The formula can be quite easily modified to get unique random strings including any and many (more than three) alphanumeric characters.