Hi,
Im trying to figure out how to display a number from an array where it has been chosen by a random number.

I have got an array with 10 different numbers (always different when program is ran).

Each index is added with the previous index number
So lets say
0 = 10
1 = 30 (+20)
2 = 32 (+2)
3 = 40 (+8)
4 = 71 (+31)
5 = 76 (+5)
6 = 100 (+24)
7 = 152 (+52)
8 = 163 (+11)
9 = 200 (+37)

What I have done in addition to this is make the index number 9 a total value, so that a random number can be generated from 0-200.

Now
If the random number is 150, the program should display the value from index number 7 which is 152.

If the random number is 70, the program should display the value from index number 4 which is 71.

(I am looping this 10 times, so 10 random numbers are generated.)

This is where the problem comes in I can't seem to display the value from index 7.

Can someone point me in the right direction please. Would appreciate it a lot.

This is where I think the problem is coming but can't seem to figure it out

``````for (i=0; i < pop; i++)
{
randomno = (rand()%total);
cout<<"Random Number             = "<<randomno<<endl;
for (j=0; j < pop; j++)
if (randomno > running_tot[i])
for (k=0; k < chro; k++)
{
par[i][k] = mosome[j][k];
j = pop + 1;
cout<<"Par ["<<i<<"]         = "<<par[i];
cout<<endl;
}
}``````

(if the post is unclear, please say so and I will try elaborate abit more)

Thanks

## All 8 Replies

``````for (i=0; i < pop; i++)
{
randomno = (rand()%total);
cout<<"Random Number             = "<<randomno<<endl;
for (j=0; j < pop; j++)
if (randomno > running_tot[i])
for (k=0; k < chro; k++)
{
par[i][k] = mosome[j][k];
j = pop + 1;
cout<<"Par ["<<i<<"]         = "<<par[i];
cout<<endl;
}
}``````

1) What is pop?
2) What is running_tot?
3) What is chro?
4) Is par 2d array or 1d array?

A full source-coude would be nice.

``````for (i=0; i < pop; i++)
{
randomno = (rand()%total);
cout<<"Random Number             = "<<randomno<<endl;
for (j=0; j < pop; j++)
if (randomno > running_tot[i])
for (k=0; k < chro; k++)
{
par[i][k] = mosome[j][k];
j = pop + 1;
cout<<"Par ["<<i<<"]         = "<<par[i];
cout<<endl;
}
}``````

1) What is pop?
2) What is running_tot?
3) What is chro?
4) Is par 2d array or 1d array?

A full source-coude would be nice.

1) Pop is defined as 10. (#define pop 10)
2) running_tot is the array
3) chro is used in another part of my program which holds another array.
4) par will become a new array from the numbers selected by the random number. - 2d or 1d,currently I want it as 1d but will later change it to 2d.

``````void roulette_wheel(int fitness[population], char chromosome[population][chromelength], char parent[population][chromelength])
{
int i;
int j;
int k;
int total_fitness;
int running_total[population];
int random_integer;
int current_total;

current_total=0;
cout<<endl;

for (i=0; i < population; i++)
{
current_total = fitness[i] + current_total;
running_total[i] = current_total;
cout<<"Running Total ["<<i<<"]         = "<<running_total[i];
cout<<endl;
}
cout<<endl;
total_fitness = current_total;
cout<<"Total Fitness             = "<<total_fitness<<endl;
cout<<endl;

for (i=0; i < population; i++)
{
random_integer = (rand()%total_fitness);
cout<<"Random Number             = "<<random_integer<<endl;
for (j=0; j < population; j++)
if (random_integer > running_total[i])
for (k=0; k < chromelength; k++)     //#define chromelength 5
{
parent[i][k] = chromosome[j][k];
j = population + 1;
cout<<"Parent ["<<i<<"]         = "<<parent[i];
cout<<endl;
}
}
}``````

Problem I get is that either the program runs and the crashes. or it will display values that are totally irrelevant, such as squares etc.

Hi,
Im trying to figure out how to display a number from an array where it has been chosen by a random number.

what are you using to generate randome number ??? the array or the rand function???
what do u exactly want??

Now
If the random number is 150, the program should display the value from index number 7 which is 152.

If the random number is 70, the program should display the value from index number 4 which is 71.

(I am looping this 10 times, so 10 random numbers are generated.)

again you are generating random numbers and comparing them to array element?? what do u want???what's the purpose of array??

(if the post is unclear, please say so and I will try elaborate abit more)

what are you using to generate randome number ??? the array or the rand function???
what do u exactly want??

again you are generating random numbers and comparing them to array element?? what do u want???what's the purpose of array??

I am using rand()%total_fitness to generate a random number from 0-total_fitness.

0 = 10
1 = 30 (+20)
2 = 32 (+2)
3 = 40 (+8)
4 = 71 (+31)
5 = 76 (+5)
6 = 100 (+24)
7 = 152 (+52)
8 = 163 (+11)
9 = 200 (+37)

total_fitness = the last number in the array, this being 200.
the array is running_total.

Lets say that the generated random number is 150

It will go through the array:
0 = 10
1 = 30 (+20)
2 = 32 (+2)
3 = 40 (+8)
4 = 71 (+31)
5 = 76 (+5)
6 = 100 (+24)
7 = 152 (+52)
8 = 163 (+11)
9 = 200 (+37)

If the random number is less than the number in the array, it will display that number. In this case 150 is not less than index 0, 1, 2, 3, 4, 5, 6 but it is less than index 7, so it will display the value of index 7 which is 152.

Why don't loop from the first index till the last index, and output only index that is bigger than the random number.

I believe you are using the wrong index in the line `if (random_integer > running_total[i])` Shouldn't that be `running_total[B][j][/B]` ?

Also, please work in your formatting consistency. Your code is just a little difficult to follow. See this.

Help me code OQL!Help me help me.Send to my Email: daohaoquang@yahoo.com. Thanks you very much.

Please I need solution this array:

Write a C program that roll a single six-sided die 10 times. Produce a random number from 1 through 6 as
shown in the following frequency distribution. (Hint: use an array of counter)