Robot Framework:生成唯一随机数

编程入门 行业动态 更新时间:2024-10-13 16:21:05
本文介绍了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:生成唯一随机数

本文发布于:2023-11-30 07:11:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649127.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:随机数   Robot   Framework

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!