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:
random vector generation within specific values

Subject: random vector generation within specific values

From: john

Date: 22 Jul, 2010 20:03:26

Message: 1 of 7

I want to generate a random vector with the values in between -2.45 and +2.45 but no values less than +0.65 or -0.65? I would appreciate any help in this regard.

Gerard

Subject: random vector generation within specific values

From: Walter Roberson

Date: 22 Jul, 2010 20:40:41

Message: 2 of 7

john wrote:
> I want to generate a random vector with the values in between -2.45 and +2.45 but no values less than +0.65 or -0.65? I would appreciate any help in this regard.

Do you mean that you want to generate over the union of the intervals
[-2.45, -0.65], [0.65, 2.45]

??

If so please be specific about whether each of the boundaries should be "open"
or "closed".

For an N x M array of random values, open intervals:

(0.65 + (2.45 - 0.65) * rand(N,M)) .* (2 * (rand(N,M) > 0.5) - 1)


It is possible to use only a single random number per location, but to do so
without using a named temporary variable would require an anonymous function,
probably needing to create an unnamed temporary matrix.

Subject: random vector generation within specific values

From: someone

Date: 22 Jul, 2010 20:57:04

Message: 3 of 7

john <mushtaq.jaffar@yahoo.com> wrote in message <786466933.38493.1279829037211.JavaMail.root@gallium.mathforum.org>...
> I want to generate a random vector with the values in between -2.45 and +2.45 but no values less than +0.65 or -0.65? I would appreciate any help in this regard.
>
> Gerard

I think you need to restate you problem.

If I interpret the above literally doesn't that just mean generate random numbers between +0.65 and +2.45?

% In any event:
doc rand
% and look at Example 5.

If you are trying to generate a random number between two extremes with a "hole" in the middle, I see two ways to procede (and I'm sure there are many others).

1. Generate a random number between the two extremes and throw out any number that "falls in the hole" and try again until it doesn't, {This method may require multiple (who knows how many?) random draws per try} or

2. Generate two random numbers, one in the lower extreme and one in the higher extreme. And then randomly pick ONE of them. {This method assures you only need two tries per draw.}

Subject: random vector generation within specific values

From: john

Date: 22 Jul, 2010 21:10:54

Message: 4 of 7

kindly find here the code i want to implement:

%%% CODE

search_Range=[-2.45 +2.45] % here i want to mention values between -2.45 and +2.45 but no values less than +0.65 or -0.65

pop_Size= 10;

indiv_Size = 30;

population(1:pop_Size,1:indiv_Size)=search_Range(1)+(search_Range(2)-search_Range(1))*rand(pop_Size,indiv_Size);

Subject: random vector generation within specific values

From: Walter Roberson

Date: 22 Jul, 2010 21:28:56

Message: 5 of 7

john wrote:
> kindly find here the code i want to implement:
>
> %%% CODE
>
> search_Range=[-2.45 +2.45] % here i want to mention values between -2.45 and +2.45 but no values less than +0.65 or -0.65

We are having difficulty figuring out what you mean by "but no values less
than +0.65 or -0.65"


> pop_Size= 10;
>
> indiv_Size = 30;
>
> population(1:pop_Size,1:indiv_Size)=search_Range(1)+(search_Range(2)-search_Range(1))*rand(pop_Size,indiv_Size);


Unless the population matrix already exists and you only want to change part
of it, you do not need the indices on the left side:

population=search_Range(1)+(search_Range(2)-search_Range(1))*rand(pop_Size,indiv_Size);


If you are trying to create a range that has a "hole" in it, and the range is
symmetric, randomly take the negative of about half of the array (after
setting search_range(1) to 0.65). Or just use the code I provided earlier.

Subject: random vector generation within specific values

From: someone

Date: 23 Jul, 2010 00:33:04

Message: 6 of 7

john <mushtaq.jaffar@yahoo.com> wrote in message <1592537430.38822.1279833090745.JavaMail.root@gallium.mathforum.org>...
> kindly find here the code i want to implement:
>
> %%% CODE
>
> search_Range=[-2.45 +2.45] % here i want to mention values between -2.45 and +2.45 but no values less than +0.65 or -0.65

That doesn't make much sense.
Do you mean no values BETWEEN +0.65 and -0.65?

>
> pop_Size= 10;
>
> indiv_Size = 30;
>
> population(1:pop_Size,1:indiv_Size)=search_Range(1)+(search_Range(2)-search_Range(1))*rand(pop_Size,indiv_Size);

Subject: random vector generation within specific values

From: Roger Stafford

Date: 23 Jul, 2010 01:27:03

Message: 7 of 7

john <mushtaq.jaffar@yahoo.com> wrote in message <1592537430.38822.1279833090745.JavaMail.root@gallium.mathforum.org>...
> kindly find here the code i want to implement:
>
> %%% CODE
>
> search_Range=[-2.45 +2.45] % here i want to mention values between -2.45 and +2.45 but no values less than +0.65 or -0.65
>
> pop_Size= 10;
>
> indiv_Size = 30;
>
> population(1:pop_Size,1:indiv_Size)=search_Range(1)+(search_Range(2)-search_Range(1))*rand(pop_Size,indiv_Size);
- - - - - - - - - - -
  John, you have twice said "but no values less than +0.65 or -0.65", but that seems a careless way to state things since it is surely not what you really mean nor is it even good English. This has created needless confusion in this thread as to what you do mean. Perhaps you mean "but no values between +0.65 and -0.65" which is the "hole in the center" interpretation others are referring to. You should think carefully before putting out sloppy statements like the above in a newsgroup where it can be read by hundreds of others. It makes it less likely that you will get useful help.

  Assuming the hole-in-the-center interpretation is correct, you could do it this way:

 a = 2.45;
 b = 0.65;
 t = rand(m,n); % For m x n of them
 r = 2*(a-b)*t-a+2*b*(t>=.5); % r lies either in [-a,-b] or in [b,a]

(This is similar to Walter's but calls on rand only once per data point.)

Roger Stafford

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