Robot Framework: Generating Unique Random number

后端 未结 5 1169
失恋的感觉
失恋的感觉 2021-01-18 21:46

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

${random}   Generate Ran         


        
相关标签:
5条回答
  • 2021-01-18 21:59

    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

    0 讨论(0)
  • 2021-01-18 22:08

    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
    
    0 讨论(0)
  • 2021-01-18 22:19
    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.

    0 讨论(0)
  • 2021-01-18 22:22

    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
    
    0 讨论(0)
  • 2021-01-18 22:25

    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}  
    
    0 讨论(0)
提交回复
热议问题