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.
In a column, use =RAND() formula to generate a set of random numbers between 0 and 1.
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.
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.
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.
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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With