# Thread: Matlab - Random Number between 1 .. n

1. ## Matlab - Random Number between 1 .. n

Does anyone here know how I make a random number generator in matlab
to choose a natural number from (1,..., n)?

(edit: I found something wich kind of does what I want...N=rand(k,1) makes a vector of k random numbers in (0,1). Then N=round(n*rand(k,1)) creates a vector of k numbers in {1,...,n}. However this could be done better right?)

2. Originally Posted by Dinkydoe
Does anyone here know how I make a random number generator in matlab
to choose a natural number from (1,..., n)?

(edit: I found something wich kind of does what I want...N=rand(k,1) makes a vector of k random numbers in (0,1). Then N=round(n*rand(k,1)) creates a vector of k numbers in {1,...,n}. However this could be done better right?)
That does not produce a uniform distribution over 1, ... , n. Try:

N=ceil(n*rand(k,1));

CB

3. Yes, thanks. I realised that later ;p