Mercurial > octave-nkf
comparison src/DLD-FUNCTIONS/rand.cc @ 14688:42edbe6f6a8d stable
Fix bad two-arg call of randperm (bug #36535)
* rand.cc (Frandperm): Rewrite algorithm for short_shuffle. Add another test.
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Fri, 25 May 2012 11:24:02 -0400 |
parents | 72c96de7a403 |
children | 52c5fb67fa5f |
comparison
equal
deleted
inserted
replaced
14686:847ed7f603cf | 14688:42edbe6f6a8d |
---|---|
1084 for (octave_idx_type i = 0; i < m; i++) | 1084 for (octave_idx_type i = 0; i < m; i++) |
1085 { | 1085 { |
1086 octave_idx_type k = i + | 1086 octave_idx_type k = i + |
1087 gnulib::floor (rvec[i] * (n - i)); | 1087 gnulib::floor (rvec[i] * (n - i)); |
1088 | 1088 |
1089 if (map.find(k) == map.end()) | 1089 //For shuffling first m entries, no need to use extra |
1090 //storage | |
1091 if (k < m) | |
1090 { | 1092 { |
1091 map[k] = ivec[i]; | 1093 std::swap (ivec[i], ivec[k]); |
1092 ivec[i] = k; | |
1093 } | 1094 } |
1094 else | 1095 else |
1095 std::swap (ivec[i], map[k]); | 1096 { |
1096 | 1097 if (map.find (k) == map.end ()) |
1098 map[k] = k; | |
1099 | |
1100 std::swap (ivec[i], map[k]); | |
1101 } | |
1097 } | 1102 } |
1098 } | 1103 } |
1099 else | 1104 else |
1100 { | 1105 { |
1101 | 1106 |
1124 | 1129 |
1125 return retval; | 1130 return retval; |
1126 } | 1131 } |
1127 | 1132 |
1128 /* | 1133 /* |
1129 %!assert(sort (randperm (20)),1:20) | 1134 %!assert (sort (randperm (20)), 1:20) |
1130 %!assert(length (randperm (20,10)), 10) | 1135 %!assert (length (randperm (20,10)), 10) |
1136 | |
1137 %!test | |
1138 %! rand ("seed", 0); | |
1139 %! for i = 1:100 | |
1140 %! p = randperm (305, 30); | |
1141 %! assert (length (unique (p)), 30); | |
1142 %! endfor | |
1131 */ | 1143 */ |