I have to generate unique random numbers in robot framework. I have used \"Generate Random String\" to get random numbers. Command:
${random} Generate Ran
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
Generate 4 Random And Unique Numbers
${numbers} Create List 1 2 3 4 5
... 6 7 8 9 10
${random index} Generate Random String 1 0123456789
${random number 1} Set Variable @{numbers}[${random index}]
Remove From List ${numbers} ${random index}
${random index} Generate Random String 1 012345678
${random number 2} Set Variable @{numbers}[${random index}]
Remove From List ${numbers} ${random index}
${random index} Generate Random String 1 01234567
${random number 3} Set Variable @{numbers}[${random index}]
Remove From List ${numbers} ${random index}
${random index} Generate Random String 1 0123456
${random number 4} Set Variable @{numbers}[${random index}]
Remove From List ${numbers} ${random index}
${random numbers} Create List ${random number 1} ${random number 2} ${random number 3} ${random number 4}
[Return] ${random numbers}
or with python
import random
def generate_4_random_and_unique_numbers():
pool = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
random_numbers = []
for index in range (0, 4):
index = random.randint(0, len(pool)-1)
random_numbers.append(pool[index])
del pool[index]
return random_numbers
In general, create a list of possible numbers. Pick number randomly from that list and then delete that number from the list. Repeat as many times as needed.
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 added these 2 lines and it worked fine:
* Test Cases *
Generate XYZ Random number
${rand_XYZ}= generate random string 9 [NUMBERS]
log to console ${rand_XYZ}