0

I want to generate random number in imacros from 1-20, but do not REPEAT, I have a code, anybody can modified it?

I Have datasource file data.csv, it has 1x1 rows/columns and has 20 lines data.

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=*
The Archetypal Paul
  • 41,321
  • 20
  • 104
  • 134

3 Answers3

1

In Java you can do.

List<Integer> ints = new ArrayList<Integer>();
for (int i = 1; i <= 20; i++) ints.add(i);
Collections.shuffle(ints);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

Put here somewhere a while loop and you could get a working code. Taken from:

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;
}
Community
  • 1
  • 1
edinvnode
  • 3,497
  • 7
  • 30
  • 53
0

In java this will generate random number between 1 to 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);
 }
}
SparkOn
  • 8,806
  • 4
  • 29
  • 34