Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Distance randomly generated with density function

Subject: Distance randomly generated with density function

From: Aggelos

Date: 19 Nov, 2011 13:57:10

Message: 1 of 4

Hello everyone,

I have the density function f(d)=2d, where d is the distance of a user and 0<=d=<1 (kilometers). My system has 64 users and i want the distance d of a user to be generated randomly with the above density function. I have programmed a code but i believe it's not responding in my needs. Can someone explain me how can i simulate this ? My code is following.

PROB=[.01 .03 .05 .07 .09 .11 .13 .15 .17 .19] ;
temporary = randp(PROB,N);
d=zeros(1,N);
d=0.1.*temporary(1,:);

Subject: Distance randomly generated with density function

From: Aggelos

Date: 19 Nov, 2011 14:07:07

Message: 2 of 4

"Aggelos" wrote in message <ja8cjm$11f$1@newscl01ah.mathworks.com>...
> Hello everyone,
>
> I have the density function f(d)=2d, where d is the distance of a user and 0<=d=<1 (kilometers). My system has 64 users and i want the distance d of a user to be generated randomly with the above density function. I have programmed a code but i believe it's not responding in my needs. Can someone explain me how can i simulate this ? My code is following.
>
> PROB=[.01 .03 .05 .07 .09 .11 .13 .15 .17 .19] ;
> temporary = randp(PROB,N);
> d=zeros(1,N);
> d=0.1.*temporary(1,:);

Just forget my code. It's absolutely rubbish. I want the distance to be a random number (according to the density function) between 1meter and 999 meters, not just 100, 200, 300...800, 900 and 1000 as the my code does.

Subject: Distance randomly generated with density function

From: Roger Stafford

Date: 19 Nov, 2011 18:46:29

Message: 3 of 4

"Aggelos" wrote in message <ja8cjm$11f$1@newscl01ah.mathworks.com>...
> I have the density function f(d)=2d, where d is the distance of a user and 0<=d=<1
- - - - - - - -
  Is this what you are thinking about?

 d = sqrt(rand(n,1));

The quantity d is distributed over the interval x = [0,1] with a density 2*x.

Roger Stafford

Subject: Distance randomly generated with density function

From: Aggelos

Date: 19 Nov, 2011 19:08:11

Message: 4 of 4

"Roger Stafford" wrote in message <ja8ti5$j7d$1@newscl01ah.mathworks.com>...
> "Aggelos" wrote in message <ja8cjm$11f$1@newscl01ah.mathworks.com>...
> > I have the density function f(d)=2d, where d is the distance of a user and 0<=d=<1
> - - - - - - - -
> Is this what you are thinking about?
>
> d = sqrt(rand(n,1));
>
> The quantity d is distributed over the interval x = [0,1] with a density 2*x.
>
> Roger Stafford

That's exactly what i want :D I was so confused with the method of inversing the cumulative function, but it was so simple... I am new to Matlab as you can understand. Thank you very much Roger.

My best regards

Aggelos

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us