我想在1-20的imacros中生成随机数,但是没有REPEAT,我有一个代码,任何人都可以修改它吗?
我有数据源文件data.csv,它有1x1行/列,有20行数据。
SET !DATASOURCE data.csv
SET !VAR2 EVAL("var randomNumber=Math.floor(Math.random()*20 + 1); randomNumber;")
SET !DATASOURCE_LINE {{!VAR2}}
EVENT TYPE=CLICK SELECTOR="HTML>BODY>DIV>DIV:nth-of-type(2)>DIV>DIV>DIV>DIV>DIV:nth-of-type(3)>DIV>FORM>TEXTAREA" BUTTON=0
EVENTS TYPE=KEYPRESS SELECTOR="HTML>BODY>DIV>DIV:nth-of-type(2)>DIV>DIV>DIV>DIV>DIV:nth-of-type(3)>DIV>FORM>TEXTAREA" CHARS="{{!COL1}}"
TAG POS=1 TYPE=INPUT:SUBMIT FORM=NAME:NoFormName ATTR=*
答案 0 :(得分:1)
在Java中你可以做到。
List<Integer> ints = new ArrayList<Integer>();
for (int i = 1; i <= 20; i++) ints.add(i);
Collections.shuffle(ints);
答案 1 :(得分:1)
在这里放一个while循环,你可以得到一个有效的代码。摘自:
Generating random whole numbers in JavaScript in a specific range?
var array_of_numbers=null;
var random_number = null;
random_number=getRandomArbitrary(1,20);
if(array_of_numbers.indexOf(random_number)>0)
{
random_number=getRandomArbitrary(1,20);
}
else
{
array_of_numbers.push(random_number);
}
/**
* Returns a random number between min (inclusive) and max (exclusive)
*/
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
/**
* Returns a random integer between min (inclusive) and max (inclusive)
* Using Math.round() will give you a non-uniform distribution!
*/
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
答案 2 :(得分:0)
在java中,这将生成1到20之间的随机数
ArrayList<Integer> arrOfRandomNumber = new ArrayList<Integer>();
Random randomGenerator = new Random();
while (arrOfRandomNumber.size() < 20) {
int random = randomGenerator .nextInt(20);
if (!arrOfRandomNumber.contains(random+1)) {
arrOfRandomNumber.add(random+1);
}
}