I am trying to test a program which checks for correct use of brackets. To do this I want to generate Long strings of numbers with correct use of brackets (to test incorrect uses I can easily just swap around individual characters or groups of characters)
I can easily generate a vector of non-bracket characters. However, To place brackets correctly I need n
unique indices. I do not know of a way to efficiently generate random indices (a list of unique random numbers in a range) so I would like either a way to do that or a way of converting random numbers into unique indices.
The two best approaches I can think of for this are:
- generate a vector of all indices
0-N
, shuffle it and then take the firstn
- generate a random number, test if it is in a hashset, if isn't add it and if it is regenerate until it isn't. Repeat until the hashset has
n
members then convert it to a vector
The first approach is efficient for N = n + $\epsilon$
but very inefficient for N >> n
while the second approach is very inefficient for N = n + $\epsilon$
but efficient for N >> n
.
Note:
n
is the number of bracketsN
is the number of characters- for those unfamiliar with the notation
$\epsilon$
is some small positive number
Aucun commentaire:
Enregistrer un commentaire