Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Robot Framework: Generating Unique Random number

I have to generate unique random numbers in robot framework. I have used "Generate Random String" to get random numbers. Command:

${random}   Generate Random String  1   [NUMBERS])

I gave the above statement in a for loop. Now I am able to get 'n' random numbers. But they are not unique. How do I make them unique?

Whats my case exactly: I want four unique random numbers ranging from 1 to 10. I am trying to give the generate random number command in for loop and in the second loop i am trying to compare it with first random value and so on. But this is not working.

Is there any simple logic to get 4 unique random numbers between 1 to 10?

Thanks in advance.

like image 557
venugopal Avatar asked Mar 20 '14 06:03

venugopal


People also ask

How do you generate unique random numbers?

In a column, use =RAND() formula to generate a set of random numbers between 0 and 1.

How do you get random values from a list in Robot Framework?

The simplest solution is to call python's random. choice method with the built-in Evaluate keyword. You can use robot's extended variable syntax to pass the list of choices into the function.

How do you generate a unique list of random numbers in Python?

To generate random numbers in Python, randint() function of random module is used. randint() accepts two parameters: a starting point and an ending point. Both should be integers and the first value should always be less than the second.

How do you generate unique random numbers in CPP?

rand() rand() function is an inbuilt function in C++ STL, which is defined in header file <cstdlib>. rand() is used to generate a series of random numbers. The random number is generated by using an algorithm that gives a series of non-related numbers whenever this function is called.


3 Answers

I think the random module's sample function is the most elegant solution. Here taking 4 out of the set of numbers from 1 to 10 inclusive:

${numbers}=    Evaluate    random.sample(range(1, 11), 4)    random

This returns a list of int's. If you want string representation of numbers...

${numbers}=    Evaluate    random.sample([unicode(x) for x in range(1, 11)], 4)    random
like image 78
ombre42 Avatar answered Oct 05 '22 18:10

ombre42


I think you should try below code. It will give you a 4 digit unique random number from "0123456789"

library String

${PO_Number}    Generate random string    4    0123456789

I hope it will work for you as it worked for me.

Regards, Arvind Kumar

like image 42
user11733717 Avatar answered Oct 05 '22 18:10

user11733717


In order to get 4 random numbers from 1 to 10:

${Random Numbers}=     Evaluate  random.sample(range(1, 10),4)   random

Syntax:

${Random Numbers}=     Evaluate  random.sample(range(specify the range of number),(number of random numbers to be generated))    random
like image 26
tharuni sai Avatar answered Oct 05 '22 18:10

tharuni sai