随机化特定范围 [英] Randomizing specific range

查看:61
本文介绍了随机化特定范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Friends,

我正在为特定范围进行随机化。我的方法如下所述。

Hello Friends,
I am doing randomization of intergers for specific range.The way i have done is mentioned below.

展开 | 选择 | Wrap | 行号

推荐答案

因为你在标量上下文中使用数组@id,这是数组的长度,而不是数组中的元素。数组长度为15,因此代码返回的值将为0到14.您要做的是使用数组的长度生成一个随机数,用作数组的索引号,它将返回相应的值在数组中。使用代码比使用单词更容易解释:

Its because you are using the array @id in scalar context which is the length of the array, not the elements in the array. The array is length 15 so the values returned by your code will be 0 thru 14. What you want to do is use the length of the array to generate a random number to use as an index number of the array which will return the corresponing values in the array. Easier explained with code than words:

展开 | 选择 | Wrap | 行号


我喜欢Kevin的代码。但是,这将生成一个介于4和28之间的随机数。仅举例来说,我打印了100次。

I like Kevin''s code. However, this will produce a random number between 4 and 28. Just for an example I am printing it 100 times.

展开 | 选择 | 换行 | 行号


谢谢kelvin,

在那段代码中,如果我想生成idx1,idx2,idx3等的随机值,那么......这就是idx1生成的随机值不等于任何索引,即唯一和idx2生成的随机值不等于索引中的任何一个..也就是说

随机值必须是唯一的,即idx1 != idx2!= idx3 ..so on .....

如果我运行100或1000次......范围内的值不能相同(4..30) 。



谢谢

Raghavendra
Thank you kelvin,
In that code, if i want to generate random values for idx1,idx2,idx3....so on..such that the random value generated by idx1 not equal to anyone of the indexes i.e unique and the random value generated by idx2 not equal to anyone of the indexes ..so on i.e
the random values must be unique i.e idx1 != idx2 != idx3 ..so on.....
if i run 100 or 1000 times.....the values must not be same for the range (4..30).


Thanks
Raghavendra


这篇关于随机化特定范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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