Line | |
---|
1 | %diff_rand_ints Add n random ints in some scope to a given vector. |
---|
2 | % |
---|
3 | % Add n ints, randomly chosen between from and to inclusive to the list in |
---|
4 | % ints. The resulting vector of ints should have all different numbers. |
---|
5 | % |
---|
6 | % y = diff_rand_ints(ints, n,from,to) |
---|
7 | |
---|
8 | function y = diff_rand_ints(ints, n,from,to) |
---|
9 | |
---|
10 | if (to + 1 - from) < n + size(ints,2) |
---|
11 | error(sprintf('Not enough room for %d random numbers from %d to %d', n, from, to)); |
---|
12 | end |
---|
13 | for i = 1:n |
---|
14 | x = random_int(from,to-size(ints,2)); |
---|
15 | oldx = from - 1; |
---|
16 | while oldx < x |
---|
17 | temp = x; |
---|
18 | x = x + sum((oldx < ints) & (ints <= x)); |
---|
19 | oldx = temp; |
---|
20 | end |
---|
21 | ints = [ints,x]; |
---|
22 | end |
---|
23 | y = ints; |
---|
Note: See
TracBrowser
for help on using the repository browser.