Sub CreateRND()
Dim arr() As Integer 'Define array
ReDim arr(range("d3").Value) 'Change the array size
Dim min As Integer 'Define the minimum value of a random number
Dim max As Integer 'Define the maximum value of random numbers
Dim flag As Boolean 'Define flag variable to determine whether there are duplicate values
max = range("d2").Value 'Assign the value of cell d2 to the maximum value
min = range("d1").Value 'Assign the value of cell d1 to the minimum value
If (max - min 1
Exit Sub
End If
Randomize (Now()) 'Use the current time to generate a random number seed
For i = 0 To range("d3").Value 'Loop to generate random numbers
Do
arr(i) = Rnd() * (max - min) min 'Generate random numbers
flag = False
For j = 0 To (i - 1) 'Loop to determine whether the current random number is the same as the previously generated random number, and if it is the same, regenerate it
If (arr(i) = arr(j)) Then
flag = True
End If
Next
Loop While flag
Next
Columns("A:A").ClearContents
range("a1").Resize(range("d3").Value) = Application.Transpose(arr) 'Output result
End Sub
1. First calculate the difference in this range. It is assumed here that the range value is a random number within 50~100
2. Open the spreadsheet and enter the following formula in the cell where you want to generate random numbers: =rand()*(100-50) 50. Press the confirm button to generate a random number within the specified range!
3. If you need to generate multiple random numbers, just drag the small triangle in the bottom corner of the cell!
Microsoft Excel is one of the components of Microsoft Office software. It is a spreadsheet software written and run by Microsoft for computers with Windows and Apple Macintosh operating systems. The earliest version was developed in 1985. .
Microsoft Excel can perform various data processing, statistical analysis and auxiliary decision-making operations, etc. It has the functions of opening or closing table header rows, calculated columns using a single formula to adjust each row, data sorting and filtering, custom formulas and text input, etc. Function. At present, Microsoft Excel is widely used in many fields such as management, statistics and finance, and finance.
The above is the detailed content of How to generate random numbers for roommates using Excel. For more information, please follow other related articles on the PHP Chinese website!