Robot Framework: Generating Unique Random number

后端 未结 5 1167
失恋的感觉
失恋的感觉 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条回答
  •  -上瘾入骨i
    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.

提交回复
热议问题