How to random select csv value

Hi all,

I import a test.csv file

Example:

www,sssr,eded,fdff
123,232,324
aaa,bbb,ccc,ddd,eeeed,bgd
aas,bbd,cca

I want to random select type from Line1,id=A1 Math.floor(Math.random()*4
then random select type from Line2 id=A2 Math.floor(Math.random()*3
then random select type from Line3 id=A3 Math.floor(Math.random()*6
then random select type from Line4 id=A4 Math.floor(Math.random()*3

How to solve?
pls help,thanks!

  1. Generate a random value using storeEval

  2. Assign the value to !csvReadLineNumber

    storeEval | Math.floor(Math.random()*11) | myRandomValue
    store | ${myRandomValue} | !csvReadLineNumber
    csvRead | test.csv

=> this reads a random row from your CSV file.

See also How to resume kantu from where it left off if it lost connection with db? (the sample macro in this post uses !csvReadLineNumber, too)

1 Like

If you also want to select the column randomly, it gets a bit more complicated, but a solution to access ${!COL1}, {!COL2 },… based on an index can be found here: Using excel value with loop - not printing values - printing col numbers

Thanks,very useful
I solve it like this.

change the csv row to column

www,sssr,eded,fdff

123,232,324

aaa,bbb,ccc,ddd,eeeed,bgd

aas,bbd,cca

to

www,123,aaa,aas
sssr,232,bbb,bbd,
eded,324,ccc,cca
fdff,0,ddd,0
0,0,eeeed,0
0,0,dgd,0

{
“CreationDate”: “2018-9-19”,
“Commands”: [
{
“Command”: “storeEval”,
“Target”: “Math.floor(Math.random()*4)+1”,
“Value”: “myRandomValue”
},
{
“Command”: “store”,
“Target”: “${myRandomValue}”,
“Value”: “!csvReadLineNumber”
},
{
“Command”: “csvRead”,
“Target”: “test.csv”,
“Value”: “”
},
{
“Command”: “type”,
“Target”: “id=A1”,
“Value”: “${!COL1}”
},
{
“Command”: “storeEval”,
“Target”: “Math.floor(Math.random()*3)+1”,
“Value”: “myRandomValue”
},
{
“Command”: “store”,
“Target”: “${myRandomValue}”,
“Value”: “!csvReadLineNumber”
},
{
“Command”: “csvRead”,
“Target”: “test.csv”,
“Value”: “”
},
{
“Command”: “type”,
“Target”: “id=A2”,
“Value”: “${!COL2}”
},
{
“Command”: “storeEval”,
“Target”: “Math.floor(Math.random()*6)+1”,
“Value”: “myRandomValue”
},
{
“Command”: “store”,
“Target”: “${myRandomValue}”,
“Value”: “!csvReadLineNumber”
},
{
“Command”: “csvRead”,
“Target”: “test.csv”,
“Value”: “”
},
{
“Command”: “type”,
“Target”: “id=A3”,
“Value”: “${!COL3}”
},
{
“Command”: “storeEval”,
“Target”: “Math.floor(Math.random()*3)+1”,
“Value”: “myRandomValue”
},
{
“Command”: “store”,
“Target”: “${myRandomValue}”,
“Value”: “!csvReadLineNumber”
},
{
“Command”: “csvRead”,
“Target”: “test.csv”,
“Value”: “”
},
{
“Command”: “type”,
“Target”: “id=A4”,
“Value”: “${!COL4}”
}
]
}

run well