Robot Framework:生成唯一随机数 [英] Robot Framework: Generating Unique Random number

查看:136
本文介绍了Robot Framework:生成唯一随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在机器人框架中生成唯一的随机数.我使用生成随机字符串"来获取随机数.命令:

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])

我在 for 循环中给出了上述语句.现在我能够得到'n'个随机数.但它们并不是独一无二的.我如何让它们独一无二?

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?

我的情况到底是什么:我想要四个唯一的随机数,范围从 1 到 10.我试图在 for 循环中给出生成随机数命令,在第二个循环中我试图将它与第一个随机值进行比较等等上.但这不起作用.

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.

有没有什么简单的逻辑可以得到 1 到 10 之间的 4 个唯一随机数?

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

提前致谢.

推荐答案

我认为随机模块的 sample 函数是最优雅的解决方案.这里从 1 到 10 的一组数字中取出 4 个:

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

这篇关于Robot Framework:生成唯一随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆