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:
i stuck on a random generator

Subject: i stuck on a random generator

From: Sid Hayes

Date: 1 Jan, 2009 22:25:03

Message: 1 of 5

Hi

Im trying to create a random generator so i can place N charges in random positions from -1 to 1.

so far, i have:

N = user to enter number of charges,

% Position N charges randomly on R.
R(1)=-1; % Boundary positions.
R(N)=1;

% N*N will give enough positions for the N charges to be placed on.
a=linspace(-1,1,N*N);
for i = 2:N-1
    R(2:N-1) = a(round(rand(1)*(N*N)))
end

Lets say we have 10 charges.
R(1) and R(10) are charges placed on the boundary.

the 8 charges in between give me the same random number for all 8 charges.

Can you help me please!
Warm Regards
Sid Hayes

Subject: i stuck on a random generator

From: Roger Stafford

Date: 1 Jan, 2009 23:24:01

Message: 2 of 5

"Sid Hayes" <jhumata@aston.ac.uk> wrote in message <gjjfrv$fb1$1@fred.mathworks.com>...
> Hi
>
> Im trying to create a random generator so i can place N charges in random positions from -1 to 1.
>
> so far, i have:
>
> N = user to enter number of charges,
>
> % Position N charges randomly on R.
> R(1)=-1; % Boundary positions.
> R(N)=1;
>
> % N*N will give enough positions for the N charges to be placed on.
> a=linspace(-1,1,N*N);
> for i = 2:N-1
> R(2:N-1) = a(round(rand(1)*(N*N)))
> end
>
> Lets say we have 10 charges.
> R(1) and R(10) are charges placed on the boundary.
>
> the 8 charges in between give me the same random number for all 8 charges.
>
> Can you help me please!
> Warm Regards
> Sid Hayes

  The trouble with your for-loop is that each trip places a single random number in every location in R from 2 to N-1. All you see is the result of the last trip through. To make it work you should have R(i) instead of R(2:N-1). Actually you don't need a for-loop. Just do this:

 R(2:N-1) = a(ceil(N^2*rand(1,N-2)));

(The use of 'ceil' prevents zero indices from appearing and the consequent complaints by matlab. Also it creates a uniform distribution in the 'a' points.)

  Note that with this method there is no guarantee that all charges will fall in different locations. That is because you are placing them among a relatively small number (100) of discrete possibilities. I calculate the odds of that happening as about 37%.

Roger Stafford

Subject: i stuck on a random generator

From: Sid Hayes

Date: 1 Jan, 2009 23:25:02

Message: 3 of 5

SORRY TYPO IN PREVIOUS ONE. NOW CORRECTED!

> Hi
>
> Im trying to create a random generator so i can place N charges in random positions from -1 to 1.
>
> so far, i have:
>
> N = user to enter number of charges,
>
> % Position N charges randomly on R.
> R(1)=-1; % Boundary positions.
> R(N)=1;
>
> % N*N will give enough positions for the N charges to be placed on.
> a=linspace(-1,1,N*N);
> for i = 2:N-1
> R(i) = a(round(rand(1)*(N*N)))
> end
>
> Lets say we have 10 charges.
> R(1) and R(10) are charges placed on the boundary.
>
> the 8 charges in between give me the same random number for all 8 charges.
>
> Can you help me please!
> Warm Regards
> Sid Hayes

Subject: i stuck on a random generator

From: Image Analyst

Date: 1 Jan, 2009 23:54:02

Message: 4 of 5

"Sid Hayes" <jhumata@aston.ac.uk> wrote in message <gjjjce$3vf$1@fred.mathworks.com>...
> SORRY TYPO IN PREVIOUS ONE. NOW CORRECTED!
>
> > Hi
> >
> > Im trying to create a random generator so i can place N charges in random positions from -1 to 1.
> >
> > so far, i have:
> >
> > N = user to enter number of charges,
> >
> > % Position N charges randomly on R.
> > R(1)=-1; % Boundary positions.
> > R(N)=1;
> >
> > % N*N will give enough positions for the N charges to be placed on.
> > a=linspace(-1,1,N*N);
> > for i = 2:N-1
> > R(i) = a(round(rand(1)*(N*N)))
> > end
> >
> > Lets say we have 10 charges.
> > R(1) and R(10) are charges placed on the boundary.
> >
> > the 8 charges in between give me the same random number for all 8 charges.
> >
> > Can you help me please!
> > Warm Regards
> > Sid Hayes
-------------------------------------------------------------
Is this what you're really attempting to do?
clc;
clear all;
answer = input('Enter number of charges:');
intAnswer = int32(answer);
% Get the random numbers, and sort them.
R = sort(2 * rand(intAnswer, 1) - 1);
% Reassign the first and last elements so that
% they are on the boundaries of -1 and +1.
R(1) = -1;
R(intAnswer) = 1;
R % Display in command window

Subject: i stuck on a random generator

From: Sid Hayes

Date: 2 Jan, 2009 00:10:04

Message: 5 of 5

Thank you Image analyst, that was exactly what i was trying to do!

Warm Regards
Sid

Tags for 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