Matlab:来自不重复范围的随机整数 [英] Matlab: random integers from a range with no repetition

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

问题描述

我想得到一个随机整数包含在范围内且不重复的数组,我使用了:

I want to get an array with randomized integer included in range with no repeat, I used :

randperm(15,3)

输出是:

8, 10, 12

此函数不使用范围,我只想从 10 中随机取值,例如

This function not use a range, I would like to random values only from 10 e.g.

推荐答案

如果你有统计工具箱,你可以使用 randsample 无需替换.

If you have the Statistics toolbox, you can use randsample without replacement.

% MATLAB R2017a
LB = 3;   % lower bound of range (integer)
UB = 17;  % upper bound of range (integer),  UB > LB
randsample(LB:UB,3,'false') 

@Wolfie的方法使用randperm 效果很好并且不需要工具箱.

LB - 1 + randperm(UB-LB+1,3)

<小时>

请注意 randi 有效非常适合范围内的均匀分布(离散均匀)整数,但它对 替换 进行采样(可以给出重复项).据我所知,这不需要工具箱,但需要结合一个过程来删除重复项并重新采样,直到所有整数都是唯一的.


Notice that randi works well for uniformly distributed (discrete uniform) integers in a range but it samples with replacement (can give duplicates). This requires no toolbox to my knowledge but would require combining with a procedure to remove duplicates and resample until all integers were unique.

randi([LB UB],3,1)

注意:如果@Wolfie 将其作为答案发布或可以使此答案社区维基,则将删除@Wolfie 的方法基于共识.

Note: will remove @Wolfie's method if @Wolfie posts it as answer or can make this answer community wiki based on consensus.

这篇关于Matlab:来自不重复范围的随机整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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