Hello,
Does anybody know how to write a command to generate a random array (let's say with 100 numbers) that contains numbers from a function that goes like y = x exp (2.5). where the range of x is given.

I hope that my question is understandable :-)

Thank you,

## All 19 Replies

Hello,
Does anybody know how to write a command to generate a random array (let's say with 100 numbers) that contains numbers from a function that goes like y = x exp (2.5). where the range of x is given.

I hope that my question is understandable :-)

Thank you,

More details please. What's random? x? You're going to have to put more details down, and no one is going to do the whole thing for you, so give it a try and explain where you are stuck.

I think you mean this:

``````#include <iostream>
#include <ctime>
using namespace std;

int main()
{
int array[100], counter = 0;
srand((unsigned)time(0));
while(counter < 100)
{
array[counter] = rand();
counter++;
}
return 0;
}``````

That's a simple solution if I understand your problem...

More details please. What's random? x? You're going to have to put more details down, and no one is going to do the whole thing for you, so give it a try and explain where you are stuck.

Do you want to see the thing that I wrote so far (copy and paste here what I wrote) ?Will that help?

I think you mean this:

``````#include <iostream>
#include <ctime>
using namespace std;

int main()
{
int array[100], counter = 0;
srand((unsigned)time(0));
while(counter < 100)
{
array[counter] = rand();
counter++;
}
return 0;
}``````

That's a simple solution if I understand your problem...

Thanks for your answer, actually, this is for generating a randon array with 100 elements. right? But how can we put a range for the array elements?

Do you want to see the thing that I wrote so far (copy and paste here what I wrote) ?Will that help?

Sure. Post it. Couldn't hurt. More importantly, be a little more specific about what you are looking for.

If you want to randomly generate number between certain range you can use :
rand()% x, where x is the maximum number.
Exp: rand()% 50, means range is 1-50.

Is this what you want??

The arry will generate unique number that not same with others in this arry

``````void RealRand()
{
for (int i=1 ; i<=100; i++) //use to store 100 integers
{
int a= (int)135 * rand()/32767+1; //generate random positon
if(pai[a]==0) //wheather this location is empty?
{
pai[a]=i; //empty ,push this number
}
else //not empty
{
for(int j=0;j<=135;j++) //compare all this arry's element
{
if(pai[j]==0) //find vacancy
pai[j]=i; //push
}//end for
}// end if
} //end for
}//end RealRand``````

sorry,the number 100 should omit 135

Sure. Post it. Couldn't hurt. More importantly, be a little more specific about
what you are looking for.

The code that I wrote is the following:

``````include <iostream>
#include <iomanip>
#include <cstdlib>
using std::cout;
using std::cin;
using std::endl;
using std::setw;

int main()
{

srand(time (0) );
double n[10];
for ( int i = 0; i <10 ; i++)
n[i] = (20.8 + rand() % 10      ) ;
for (int j = 0; j < 10; j++)
cout << setw(7) << j << setw (13) << n[j]<< endl;

return 0;
}``````

The result of this code give me random numbers between 20.8 and 30.8, which is good, but all the results are in the form 21.8 , 24.8, 27.8 for example, how can I get a number like 26.5 or 22.9, you know what I mean?

If you want to randomly generate number between certain range you can use :
rand()% x, where x is the maximum number.
Exp: rand()% 50, means range is 1-50.

Is this what you want??

Actually I tried to explain my confusion above, does it make sense?

Thanks for the help

The code that I wrote is the following:

include <iostream>
#include <iomanip>
#include <cstdlib>
using std::cout;
using std::cin;
using std::endl;
using std::setw;

int main()
{

srand(time (0) );
double n[10];
for ( int i = 0; i <10 ; i++)
n = (20.8 + rand() % 10 ) ;
for (int j = 0; j < 10; j++)
cout << setw(7) << j << setw (13) << n[j]<< endl;

return 0;
}

The result of this code give me random numbers between 20.8 and 30.8, which is good, but all the results are in the form 21.8 , 24.8, 27.8 for example, how can I get a number like 26.5 or 22.9, you know what I mean?

`rand() % 10` is going to give you an integer every time so it makes sense that everything ends with .8. If you want a decimal from 0 to 1 you can do a typecast to a double and a division:

``double x = rand () / (double) RAND_MAX;``

You can then multiply x by any factor (10 in your case), then add any number to the result (20.8 in your case) to get random numbers in the desired range (20.8 to 30.8 in your case).

`rand() % 10` is going to give you an integer every time so it makes sense that everything ends with .8. If you want a decimal from 0 to 1 you can do a typecast to a double and a division:

``double x = rand () / (double) RAND_MAX;``

You can then multiply x by any factor (10 in your case), then add any number to the result (20.8 in your case) to get random numbers in the desired range (20.8 to 30.8 in your case).

My command look like this now:

int main()
{
srand(time (0) );
double x = rand ();
double n[10];
for ( int i = 0; i <10 ; i++)
n = ( 20.8 + x*10 / (double) RAND_MAX ) ;
for (int j = 0; j < 10; j++)
cout << setw(7) << j << setw (13) << n[j]<< endl;

return 0;
}

Is this what you meant?
i am getting numbers other that (x.8) , but now the whole array have the same number! I know that the problem is because I declared x at the begining, but the thing is when I declare x within the array it gives me an error. What can I do?

My command look like this now:

int main()
{
srand(time (0) );
double x = rand ();
double n[10];
for ( int i = 0; i <10 ; i++)
n = ( 20.8 + x*10 / (double) RAND_MAX ) ;
for (int j = 0; j < 10; j++)
cout << setw(7) << j << setw (13) << n[j]<< endl;

return 0;
}

Is this what you meant?
i am getting numbers other that (x.8) , but now the whole array have the same number! I know that the problem is because I declared x at the begining, but the thing is when I declare x within the array it gives me an error. What can I do?

when I declare x within the array it gives me an error.

I'm not sure what you mean or what you did, but I assume you mean "within the loop", not "within the array". Yes, you are correct. You are getting the same numbers because this line is before the loop:

``double x = rand ();``

However it has nothing to do with you declaring x before the loop, but rather that you don't have `rand ()` anywhere inside the loop. Post the code that gives you an error please. You need to have `rand()` inside the loop.

I'm not sure what you mean or what you did, but I assume you mean "within the loop", not "within the array". Yes, you are correct. You are getting the same numbers because this line is before the loop:

``double x = rand ();``

However it has nothing to do with you declaring x before the loop, but rather that you don't have `rand ()` anywhere inside the loop. Post the code that gives you an error please. You need to have `rand()` inside the loop.

Actually I was able to fix the problem thank you, but if we now go back to my first question, I'll try to explain it in more details.

So what I was doing in the previous command is to come up with 10 random numbers betwen 20.8 and 30.8, with all numbers being equally probable to show up. Now , how can I do the same thing (getting random numbers between 20.8 and 30.8 but with the cosnstrain that the probability decreases exponentially { exp (2.5) } , does the question make sense?

Actually I was able to fix the problem thank you, but if we now go back to my first question, I'll try to explain it in more details.

So what I was doing in the previous command is to come up with 10 random numbers betwen 20.8 and 30.8, with all numbers being equally probable to show up. Now , how can I do the same thing (getting random numbers between 20.8 and 30.8 but with the cosnstrain that the probability decreases exponentially { exp (2.5) } , does the question make sense?

Before coding it, you need to have some mathematical formula that maps the range of [0, 1) to [20.8, 30.8) with the appropriate probability distribution. 0 will map to 20.8, 1 will map to 30.8, and everything else will map to something in between. Since the probability decreases the higher you go, 0.5 will map to something less than the midpoint (25.8). It's not clear to me what the distribution will be (i.e. I think you need to provide more details on the distribution specifics), but at this point, your problem is a mathematical one, not a programming problem. You need to derive that function that maps from a range of [0, 1) to [20.8, 30.8). Once you've done so, then it becomes a programming problem again.

Before coding it, you need to have some mathematical formula that maps the range of [0, 1) to [20.8, 30.8) with the appropriate probability distribution. 0 will map to 20.8, 1 will map to 30.8, and everything else will map to something in between. Since the probability decreases the higher you go, 0.5 will map to something less than the midpoint (25.8). It's not clear to me what the distribution will be (i.e. I think you need to provide more details on the distribution specifics), but at this point, your problem is a mathematical one, not a programming problem. You need to derive that function that maps from a range of [0, 1) to [20.8, 30.8). Once you've done so, then it becomes a programming problem again.

The mathematical function is y = x exp (-2.5)
where y is the probability and x is the random number, is this what you mean?
I think I need to plug this formula in my code somewhere, am I right?

The mathematical function is y = x exp (-2.5)
where y is the probability and x is the random number, is this what you mean?
I think I need to plug this formula in my code somewhere, am I right?

It's more complicated than that. Where's the 20.8? How do I know what value to map 0.1 to? I'm certainly not mapping it to 0.1 ^ -2.5, which isn't in the range of 20.8 to 30.8, or is that no longer the range?

It's more complicated than that. Where's the 20.8? How do I know what value to map 0.1 to? I'm certainly not mapping it to 0.1 ^ -2.5, which isn't in the range of 20.8 to 30.8, or is that no longer the range?

Oh, I see what you're saying,,, it makes perfect sense...
I was actually thinking of such a mathematical relation, but I can't come with anything like that, so I thought i should look on the web for some help, I got this line command, but I am not sure I know how to use it ( I will include the sourse as well) do you have any idea how this works? ( I tried including it in my code but id didn't work)

the command:
Exponential:

This generates random numbers with density exp(-x) for x>=0. The constructor takes no arguments.

Exponential E;
for (int i=0; i<100; i++) cout << E.Next() << "\n";

thank you for all your help

Oh, I see what you're saying,,, it makes perfect sense...
I was actually thinking of such a mathematical relation, but I can't come with anything like that, so I thought i should look on the web for some help, I got this line command, but I am not sure I know how to use it ( I will include the sourse as well) do you have any idea how this works? ( I tried including it in my code but id didn't work)

the command:
Exponential:

This generates random numbers with density exp(-x) for x>=0. The constructor takes no arguments.

Exponential E;
for (int i=0; i<100; i++) cout << E.Next() << "\n";

thank you for all your help

Not sure what your math/statistics/probability background is, but it looks to me like you are looking for how to map the cumulative distribution function (domain of [0, 1)) to the relevant value of x (range of [0, infinity)) for the exponential distribution where lambda = 2.5? I have no idea where the range of 20.8 to 30.8 comes from. The range you are mapping to should be [0, infinity) as far as I can tell.

Check out this link. I don't know your statistical background and whether it'll make sense. I don't know how to use the math BB code on this, so I can't really show integrals, but if you generate a number x from 0 to 1 and you want to map it to its corresponding y from 0 to infinity, you need to solve this equation for y, given a value x.

``x = 1 - e ^ (-2.5y)``

since lambda is 2.5. Your answer will have a "natural log" element in it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.