Results 1 to 5 of 5

Math Help - MATLAB - Random Number Generation

  1. #1
    Super Member
    Joined
    Dec 2008
    From
    Scotland
    Posts
    901

    MATLAB - Random Number Generation

    Hi.

    Basically I want to generate four random numbers in a certain interval, and they can't be equal (unlikely to happen, but just incase!).

    So let's imagine, that, for example, that interval is [0.5,1]. I want to generate a number in that interval, use it in a calculation, then generate another number on that interval, and use that in the same calculation, etc.

    So:

    Code:
    a1 = some number in [0.5,1]
    operation on a1
    a2 = some number in [0.5,1] not equal to a1
    operation on a2
    a3= some number in [0.5,1] not equal to a1 or a2
    operation on a3
    a4 = some number in [0.5,1] not equal to a1 or a2 or a3
    operation on a4
    I know that the 'rand' command generates a number on the interval [0 1]. Hrm. But I can't find any way to use that to meet my end.
    Last edited by Mush; February 17th 2009 at 02:25 PM.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Super Member
    Joined
    Dec 2008
    From
    Scotland
    Posts
    901
    I wrote something that works.

    Code:
    a = rand;
            if a<0.5
                a = a+0.5;
            end
            
       b = rand;
            if b<0.5
                b = b+0.5;
            end
            if b == a
                while b == a
                    b = rand;
                     if b<0.5
                b = b+0.5;
                     end
                end
            end
            
        c = rand;
            if c<0.5
                c = c+0.5;
            end
            if c == a
                while c == a
                    c = rand;
                     if c<0.5
                c= c+0.5;
                     end
                end
            end
            if c == b
                while c == b
                    c = rand;
                     if c<0.5
                c = c+0.5;
                     end
                end
            end
    It works, but I'll be glad to hear of anything simpler?!
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by Mush View Post
    Hi.

    Basically I want to generate four random numbers in a certain interval, and they can't be equal (unlikely to happen, but just incase!).

    So let's imagine, that, for example, that interval is [0.5,1]. I want to generate a number in that interval, use it in a calculation, then generate another number on that interval, and use that in the same calculation, etc.

    So:

    Code:
    a1 = some number in [0.5,1]
    operation on a1
    a2 = some number in [0.5,1] not equal to a1
    operation on a2
    a3= some number in [0.5,1] not equal to a1 or a2
    operation on a3
    a4 = some number in [0.5,1] not equal to a1 or a2 or a3
    operation on a4
    I know that the 'rand' command generates a number on the interval [0 1]. Hrm. But I can't find any way to use that to meet my end.
    Let the interval be (b,t) then somethink like:

    Code:
    function rv=RandIntvl(n,m,b,t)
    %
    %  function to produce a matrix of size nxm of random numbers
    %  uniformly distributed on (b,t)
    %
        if t<=b
           disp('error: b must be greater than t')
           rv=[];
           return
        end
     
        rv=rand(n,m);
        rv=rv*(t-b)+b;
    should do part of the trick (warning: untested code).

    CB
    Last edited by CaptainBlack; February 18th 2009 at 06:01 AM.
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Super Member
    Joined
    Dec 2008
    From
    Scotland
    Posts
    901
    [quote=CaptainBlack;268127]Let the interval be (b,t) then somethink like:

    Code:
    function rv=RandIntvl(n,m,b,t)
    %
    %  function to produce a matrix of size nxm of random numbers
    %  uniformly distributed on (b,t)
    %
        if t<=b
    
    :
    :
    How is it ensured that they are uniformly distributed? Does matlab do this by default when you create a matrix/vector of random numbers?
    Last edited by CaptainBlack; February 18th 2009 at 09:33 AM. Reason: Opps.. I seem to have mangled this
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by Mush View Post
    How is it ensured that they are uniformly distributed? Does matlab do this by default when you create a matrix/vector of random numbers?
    Code:
     
    >help rand
    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Random Number Generation Question
    Posted in the Advanced Statistics Forum
    Replies: 0
    Last Post: April 16th 2011, 02:25 PM
  2. Random Number Generation 1-100 odds
    Posted in the Statistics Forum
    Replies: 4
    Last Post: January 30th 2011, 12:01 PM
  3. Random Number Generation
    Posted in the Advanced Statistics Forum
    Replies: 1
    Last Post: April 29th 2009, 12:13 AM
  4. Random Number Generation
    Posted in the Advanced Statistics Forum
    Replies: 2
    Last Post: April 25th 2009, 03:59 PM
  5. Random Number Generation
    Posted in the Advanced Statistics Forum
    Replies: 1
    Last Post: February 25th 2009, 09:35 PM

Search Tags


/mathhelpforum @mathhelpforum