Results 1 to 3 of 3

Math Help - Matlab - Random Number between 1 .. n

  1. #1
    Senior Member Dinkydoe's Avatar
    Joined
    Dec 2009
    Posts
    411

    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?)
    Last edited by Dinkydoe; March 16th 2010 at 09:05 AM.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by Dinkydoe View Post
    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
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Senior Member Dinkydoe's Avatar
    Joined
    Dec 2009
    Posts
    411
    Yes, thanks. I realised that later ;p
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 1
    Last Post: June 3rd 2010, 10:39 AM
  2. Replies: 1
    Last Post: April 7th 2009, 09:52 AM
  3. MATLAB - Random Number Generation
    Posted in the Math Software Forum
    Replies: 4
    Last Post: February 18th 2009, 07:03 AM
  4. MATLAB: Random numbers
    Posted in the Math Software Forum
    Replies: 3
    Last Post: January 28th 2009, 12:10 PM
  5. Matlab: Number formating
    Posted in the Math Software Forum
    Replies: 1
    Last Post: April 7th 2008, 09:39 AM

Search Tags


/mathhelpforum @mathhelpforum