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 number generation

Subject: Random number generation

From: Kiril

Date: 14 Mar, 2009 17:01:01

Message: 1 of 5

Hi, everyone I'm new to Matlab and I need help...
I need to generate random number i bounds for example [-1, 50], and need to generate and floating number in bound but with some precision, for example 4 digits after point [-0.1236 125.1020]. I have to write function and when i pass precision and LB , RB to return vector with rand float numbers and have to write the same function but working with real numbers. Thanks... can some one help me ?

Subject: Random number generation

From: us

Date: 14 Mar, 2009 18:02:02

Message: 2 of 5

"Kiril"
> I need to generate random number i bounds for example [-1, 50], and need to generate and floating number in bound but with some precision, for example 4 digits after point [-0.1236 125.1020]...

one of the solutions

     n=5;
     lb=-1;
     ub=50;
     r=(ub-lb)*rand(n,1)+lb;
% but - you cannot REALLY define the precision due to fp issues
% - even if you do the typical stuff like
     np=10^4;
     rp=floor(np*r)/np;
     format long;
     [r,rp]
% - you end up with numbers like these
%{
     -0.571434345341806 -0.571500000000000
     48.189205869780714 48.189200000000000
     35.176633305564707 35.176600000000001
     38.436426148551696 38.436399999999999
     49.807719555670253 49.807699999999997
%}

us

Subject: Random number generation

From: Roger Stafford

Date: 14 Mar, 2009 18:35:03

Message: 3 of 5

"Kiril " <kkirqkov@gmail.com> wrote in message <gpgnsd$3kc$1@fred.mathworks.com>...
> Hi, everyone I'm new to Matlab and I need help...
> I need to generate random number i bounds for example [-1, 50], and need to generate and floating number in bound but with some precision, for example 4 digits after point [-0.1236 125.1020]. I have to write function and when i pass precision and LB , RB to return vector with rand float numbers and have to write the same function but working with real numbers. Thanks... can some one help me ?

  Your description of the problem is not at all clear, and I am forced to make a wild guess. You want to generate random values that are uniformly distributed on values discretely-spaced apart by some negative power of ten, as in:

 [19.1234 19.1235 19.1236 ... 125.1019 125.1020]

Does that come anywhere near what you want?

  In this particular case you could do:

 x = floor((1251020-191234+1)*rand(1,n)+191234)*.0001;

for n such random values. I will let you work out the proper generalization of this.

  You should realize that because matlab uses binary floating point numbers, it cannot exactly represent the majority of such decimal fractions. For example it cannot give you an exact 0.1 value but only one that is within round-off error of that.

Roger Stafford

Subject: Random number generation

From: Kiril

Date: 14 Mar, 2009 19:37:01

Message: 4 of 5

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gpgtcn$ra1$1@fred.mathworks.com>...
> "Kiril " <kkirqkov@gmail.com> wrote in message <gpgnsd$3kc$1@fred.mathworks.com>...
> > Hi, everyone I'm new to Matlab and I need help...
> > I need to generate random number i bounds for example [-1, 50], and need to generate and floating number in bound but with some precision, for example 4 digits after point [-0.1236 125.1020]. I have to write function and when i pass precision and LB , RB to return vector with rand float numbers and have to write the same function but working with real numbers. Thanks... can some one help me ?
>
> Your description of the problem is not at all clear, and I am forced to make a wild guess. You want to generate random values that are uniformly distributed on values discretely-spaced apart by some negative power of ten, as in:
>
> [19.1234 19.1235 19.1236 ... 125.1019 125.1020]
>
> Does that come anywhere near what you want?
>
> In this particular case you could do:
>
> x = floor((1251020-191234+1)*rand(1,n)+191234)*.0001;
>
> for n such random values. I will let you work out the proper generalization of this.
>
> You should realize that because matlab uses binary floating point numbers, it cannot exactly represent the majority of such decimal fractions. For example it cannot give you an exact 0.1 value but only one that is within round-off error of that.
>
> Roger Stafford

I try to write genetic algorithm for calculate AC motor efficiency by using induction motor equivalent circuit. And i started wit binary type of GA, but my fitness function have 5 params, (from AC motor equivalent circuit: Rs, Rr, Xls, Xlr, Xm) and algorithm doesn't work very good. So i want to rewrite it with floating number and i hope to works better. But so that for init Pop and for genetic operator i need random number generator. But the problem is what generator must generate numbers in GA params domains... The domain for Rs for example is [5, 15], and the value form field test is Rs=10.2 Ohm. So i need generator which can make random numbers in special domain with special precision after point. ( i use 4 digits after point so now). The function I need must have 3 input param:LB, RB and precision... i try to make it all day but nothing...... Do you have any ideas?

Subject: Random number generation

From: us

Date: 14 Mar, 2009 21:22:02

Message: 5 of 5

"Kiril"
> i try to make it all day but nothing...... Do you have any ideas...

...did you even try to look at what was shown to you so far - or do you just whine...

us

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