Math Help Forum

Math Help Forum Feed Site Feed

Go Back   Math Help Forum > Math Resources > Mathematics Software Discussion
Reply
 
Thread Tools Display Modes
  #1  
Old February 17th, 2009, 11:09 AM
Super Member
 
Join Date: Dec 2008
Location: Scotland
Posts: 871
Country:
Thanks: 6
Thanked 344 Times in 321 Posts
Mush is a jewel in the roughMush is a jewel in the roughMush is a jewel in the roughMush is a jewel in the rough
Default 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 03:25 PM.
Reply With Quote
Advertisement
 
  #2  
Old February 17th, 2009, 03:28 PM
Super Member
 
Join Date: Dec 2008
Location: Scotland
Posts: 871
Country:
Thanks: 6
Thanked 344 Times in 321 Posts
Mush is a jewel in the roughMush is a jewel in the roughMush is a jewel in the roughMush is a jewel in the rough
Default

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?!
Reply With Quote
  #3  
Old February 17th, 2009, 09:11 PM
CaptainBlack's Avatar
Grand Panjandrum
 
Join Date: Nov 2005
Location: South of England
Posts: 12,285
Country:
Thanks: 779
Thanked 4,005 Times in 3,230 Posts
CaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond repute
Default

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 07:01 AM.
Reply With Quote
The following users thank CaptainBlack for this useful post:
Donate to MHF
  #4  
Old February 18th, 2009, 04:33 AM
Super Member
 
Join Date: Dec 2008
Location: Scotland
Posts: 871
Country:
Thanks: 6
Thanked 344 Times in 321 Posts
Mush is a jewel in the roughMush is a jewel in the roughMush is a jewel in the roughMush is a jewel in the rough
Default

[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 10:33 AM. Reason: Opps.. I seem to have mangled this
Reply With Quote
  #5  
Old February 18th, 2009, 07:03 AM
CaptainBlack's Avatar
Grand Panjandrum
 
Join Date: Nov 2005
Location: South of England
Posts: 12,285
Country:
Thanks: 779
Thanked 4,005 Times in 3,230 Posts
CaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond repute
Default

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
Reply With Quote
The following users thank CaptainBlack for this useful post:
Donate to MHF
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump


All times are GMT -7. The time now is 08:09 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 ©2008, Crawlability, Inc.
©2005 - 2009 Math Help Forum


Math Help Forum is a community of maths forums with an emphasis on maths help in all levels of mathematics.
Register to post your math questions or just hang out and try some of our math games or visit the arcade.