Returns a matrix of random elements drawn uniformly from within a specified set. Sampling can be done with or without replacement. (This function was intended to fill the role of RANDINT in the Comm Toolbox, which I no longer possess. No claims are made for correctness or efficiency.)

Program does not work well when randomly generating numbers when drawn without replacement. This can be observed by using the program to randomly obtain values within a range. Sort the results and plot using a histogram. It appears that the limits in and the middle value of the range are preferentially treated.

clc;
clear;
close all;
for n = 1:10000
p(n,:) = myrandint(1,3,[1:23],'noreplace');
end

why don't you just use something like
ceil(rand(n,m)*t) ??
This will generate an nxm matrix with randomly chosen integers from 1 to t.

13 Feb 2008

Phillip Feldman

This is a simple but nicely crafted replacement for the randint() function. The only drawback that I can see if that rand(), which generates random double-precision floats, it being used to generate random integers. It should be possible to generate random integers without doing floating point operations.

28 Nov 2007

asim nadeem

It is an excellent function can used to generate random problems for linear programming.

20 Aug 2007

Yogesh Parte

Very good and handly function.

26 Mar 2007

Jeremy Watt

Very cool, thanks much.

10 Jun 2006

Makio Tamura

19 May 2006

Dimitris DenXreiazetai

very good, works fine, if you don't know that it is missing from the standard libraries you may seach for hours in the Help files

18 Jul 2005

Elan Barenholtz

Thanks for some very useful code. This guy is a master!