# Thread: Random Number Generator - Error

1. Originally Posted by iMalc
If that were the case, how would you change the code to make it so that the y=y*x part was not inside the for-i loop any more?
Stuck? That's because it is already is outside.

Stick with the way you've written your program, it's fine, short of the small change with adding brackets. brack's code is wrong because it doesn't use the same random numbers for each power. He also either has no idea that you need real numbers in the range (0, 1], or doesn't understand how your code so simply does that. It's also more complicated than yours and far less efficient.

BTW, you can shorten y = y * x to just y *= x
You are right...my thoughts were different...my sorry

2. Ok, I cleaned up the code and took your advice. If I understood various nuances and pet peeves, I'd be a better programmer. I do welcome any structural comments. My averages though still yield zero. I'm trying to understand what am I missing here. Do you think the problem is with the printf function?

Here is the edited code:

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main (int argc, char *argv[])
{
unsigned int iseed = (unsigned int)time(NULL);
srand (iseed);
int i, j, average; //defines i and average as integers
double x, y, ran, sum [15];
for (i=0; i<15; i++)
sum [i]=0;
for (j=0; j<1000; j++)

{
//printf ("%u\n", rand ());
x=y=(rand() / (RAND_MAX + 1.0));
for (i=0; i<15; i++)    //error here - averages should be less than 1 (bracket)
{
sum[i]+=y;
y*=x;}              //y=y*x
}
printf("The Sum of Random Numbers : %d ", sum);
for (i=0; i<15; i++)
{
average=sum[i]/j; //j values retained from the loop (1000); alternatively: sum[15]
printf("The Average of Random Numbers : %d i=%d\n",average, i);
}

return 0;
}```

3. The average is not an integer, so why do you declare the variable as such, and then print it as an integer?

4. Originally Posted by laserlight
The average is not an integer, so why do you declare the variable as such, and then print it as an integer?
See, this is why it takes me so much time to do this. I can't see simple stuff like that. I corrected that to double. But the code is still obviously wrong because I can't get a negative average and all the averages must be between 0 and 1.

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main (int argc, char *argv[])
{
unsigned int iseed = (unsigned int)time(NULL);
srand (iseed);
int i, j; //defines i and average as integers
double x, y, ran, sum [15], average;
for (i=0; i<15; i++)
sum [i]=0;
for (j=0; j<1000; j++)

{
//printf ("%u\n", rand ());
x=y=(rand() / (RAND_MAX + 1.0));
for (i=0; i<15; i++)    //error here - averages should be less than 1 (bracket)
{
sum[i]+=y;
y*=x;               //y=y*x
}
}
printf("The Sum of Random Numbers : %d ", sum);
for (i=0; i<15; i++)
{
average=sum[i]/j; //j values retained from the loop (1000); alternatively: sum[15]
printf("The Average of Random Numbers : %d i=%d\n",average, i);
}

return 0;
}```

5. Well, based on what you described in post #8, I am inclined to suggest something radical: ditch the current structure of your code.

Rather, create an array of 1000 doubles (let's name this numbers). Populate numbers with random numbers in the range [0.0, 1.0). Create another array of 1000 doubles to hold the powers of these random numbers (let's name this powers). Populate powers with 1s.

Now, implement this algorithm:
Code:
```for i in the range [1, 15]
for j in the range [0, 1000)
powers[j] = powers[j] * numbers[j]
sum the powers
compute the average
print the average```
You could of course do this slightly differently, e.g., instead of populating powers with 1s, you populate it as a copy of numbers, and then modify the power computation algorithm to account for this.

6. agrrrhr I see so many duplications of effort!!!!

7. Originally Posted by laserlight
That sounds like a "strawman complaint" because you compared an example of "standard" (or rather, a common convention of) brace placement with no indentation versus an example of "non-standard" brace placement but with indentation. If we add indentation:
Code:
```for (i= 0; i<15; i++){
sum[i]+=y;
y=y*x;
}```
then your complaint about it being "not visually obvious where the loop begins and mismatched braces can be a frequent problem" is no longer valid, or at least as valid as a complaint against your personal style.
Hi Lase...

A lot of this is in what you get used to, of course. There may be some common convention, but I've never been big on "banging the same gong" in the first place The thing is, for some reason I just can't clearly see that leaging brace right after the bracket... so I put it where I can see it.

Thanks for the response.

8. Originally Posted by CommonTater
There may be some common convention, but I've never been big on "banging the same gong" in the first place
Have you worked with other programmers on a project, especially one where more than one programmer might work on the same source file? If so, then you can recognise the benefit of "banging the same gong", at least for each given source file. Code formatting tools can alleviate this problem, but they also complicate the development process where other tools (e.g., file differencing tools) are concerned.

Originally Posted by CommonTater
The thing is, for some reason I just can't clearly see that leaging brace right after the bracket... so I put it where I can see it.
You can use another common convention, e.g.,
Code:
```for (i= 0; i<15; i++)
{
sum[i]+=y;
y=y*x;
}```

9. Originally Posted by laserlight
Have you worked with other programmers on a project, especially one where more than one programmer might work on the same source file?
Not yet. So far this is just a big ole time waster for me (read: "hobby") so differences in style have never been an issue... yet.

You can use another common convention, e.g.,
Code:
```for (i= 0; i<15; i++)
{
sum[i]+=y;
y=y*x;
}```
I actually started out that way... but it seemed like a waste of space so I eventually shifted over to the way I do it now... I suppose if it ever became an issue I could adapt.

Here's a sample of what my code looks like... As you can see I format the text very carefully and comment the heck out of everything. I'm hoping to come back to this in 5 years and still be able to interpret it...
Code:
```// add program to list
{ TCHAR     cl[MAX_PATH] = {0};   // command line
BYTE    x;                      // counter
// prevent seeing self
if(Handle == MainWind)
return 0;
// find empty slot
for (x = 1; x <= Setting.Programs; x++)
{ if (Programs[x] == NULL) // slot found
{ // get memory
Programs[x] = malloc(sizeof(PGMINFO));
memset(Programs[x],0,sizeof(PGMINFO));
// get program handle
Programs[x]->Program = Handle;
// get the program name
GetPgmName(Programs[x]->PgmName);
// signal completion
PulseEvent(LaunchEvent);