Not Yet Answered # random array

VernonDozier 2,218 dmanw100 36 Discussion Starter the universe Discussion Starter the universe VernonDozier 2,218 raul15791 27 chenzhp Discussion Starter the universe Discussion Starter the universe VernonDozier 2,218 Discussion Starter the universe VernonDozier 2,218 Discussion Starter the universe VernonDozier 2,218 Discussion Starter the universe VernonDozier 2,218 Discussion Starter the universe VernonDozier 2,218

0

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.

0

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...

0

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?

0

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?

0

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.

0

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??

0

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
```

0

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?

0

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

0

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).

0

`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).

Thanks for your reply,

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?

0

Thanks for your reply,

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

arrayit 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.

0

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?

0

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.

0

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?

0

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?

0

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";

The sourse: http://www.robertnz.net/nr02doc.htm#exponential

thank you for all your help

0

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";

The sourse: http://www.robertnz.net/nr02doc.htm#exponentialthank 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.

http://en.wikipedia.org/wiki/Exponential_distribution

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.

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

I am currently creating a simulation of a pizza ordering system in object oriented program. I have some question. the instruction and guideline is long but I will try and cut it down a lot. the instruction is to create a program that simulate a pizza restaurant using ...

```
#include <stdio.h>
int main(void)
int movieChoice;
int seatNum;
int seatPlan[5][10] = { 0 };
int rows, cols;
char space;
void seatDisplay(int a[][10]);
void seatNew(int n[][10]);
void seatDisplay(int a[][10]) { //this function shows the seat plan
for (int i = 0; i<5; i++) {
for (int j = 0; j<10; j++) ...
```

I have this tree format.

```
> myapp->
> setup.py
> tests ->
> test_myapp.py
> myapp ->
> __init__.py
> myapp.py
```

And from test_myapp.py, I am calling: `from myapp import myapp`

and when I run`python setup.py test`

I am getting `No module named 'myapp'`