简体   繁体   中英

How do I get a value randomly from a list in Jython script?

lists = portTest.lists(arg1, arg2) 

// this returns the lists from webservice in java  
//  public String[] list1; 
//  public String[] list2;public String[] list3;

// i want to get the random element in the list,
// not the first, second or any selected item. 
elementinthelist = lists.list1[0]

How do i generate the random element from the list

I am writing a testscript in Jython. I am calling the service using Grinder tool

In Python, use random.choice :

import random
elementinthelist = random.choice(lists.list1)

Choose a random integer between 0 (inclusive) and the length of the list (exclusive).

In regular Java this would look like

Random rand = new Random();
int randIx = rand.nextInt(lists.size());
Object element = lists.get(randIx);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM