Jump to content

Random Number Generator - Error

- - - - -

  • Please log in to reply
9 replies to this topic

#1
Excelsius

Excelsius

    Newbie

  • Members
  • Pip
  • 4 posts
Hey guys. I am trying to have a code that will output the average of random numbers between 0 and 1, meaning that the average will always be less than 1. I have an error in this code - missing brackets around the j loop, but I can't figure out what's wrong. I am new to coding and can't see the language as easily. Any help would be appreciated.


#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,average; //defines i and average as integers

  int j;

  double x, y;

  double ran;

  double 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

    sum[i]+=y;

    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",average, i);

}


  return 0;

}


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Can you give an example of what you're expecting to happen? It seems you think there's a logic error, but it's not clear what you're trying to do.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Excelsius

Excelsius

    Newbie

  • Members
  • Pip
  • 4 posts
Ok, let's see if this makes it clear:

I take 1000 random numbers between 0 and 1. I need two things from here:

1. Generate the sum of all these numbers.
2. Generate the AVERAGE of these numbers like this:

a. First do a regular average: add up all 1000 numbers together and then divide by 1000.
b. Now do the same, except SQUARE all the random numbers, add them up, and only then divide by 1000 to get the average.
c. Do the same ...... except do the cube this time.
...
...
...
continue like this until you hit the power of 15. So raise all of the random numbers to the power of 15, add them, and then divide by 1000 again to get an average.

Please note that the average in all these cases is going to be less than one because you're adding numbers that are less than one and get progressively smaller as you increase the power to which they are raised. This means that the generated output for the average must ALWAYS be smaller than 1, and get progressively smaller for larger and larger powers raised. My program still doesn't do this.

I hope this makes sense.

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
This generates numbers on [0, 10] interval and then divides by 10 to get [0, 1.0] interval, since you can't use % operator with double datatype. Maybe with some ugly casting and such... but let's leave it simple.
#include <iostream>
#include <iomanip>
#include <time.h>

using namespace std;

int main (int argc, char *argv[]) {

  srand ((unsigned)time(NULL));
  double sum = 0;
  double avg = 0;
  double num;
    
  for (int i = 0; i < 1000; ++i) {
    num = rand() % 11;
    num /= 10;
    sum += num;
    avg += num;
  }
  
  avg /= 1000;
  
  cout << setprecision(1);
  cout << "AVG: " << avg << endl;
  cout << setprecision(4);
  cout << "SUM: " << sum << endl;

  return 0;
}
The power part I leave to you. :)
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Also, you are NOT doing the powers within your for(i) loop, so all your sums will be identical.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
Excelsius

Excelsius

    Newbie

  • Members
  • Pip
  • 4 posts
I have revised the code. If I could just resolve the averages portion, this might just work. I am still not sure where the error is.


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

}




#7
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
x=y=(rand() / (RAND_MAX + 1.0));
You're using wrong operator here; you want to get remainder so use % operator instead. Note that you also can't use modolus operator with double (float aswell I think) datatype.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#8
Excelsius

Excelsius

    Newbie

  • Members
  • Pip
  • 4 posts
You mean like this:

x=y=(rand() % (RAND_MAX + 1.0));
?

And you're saying that would only work with int? Because I can't use int for a number between 0 and 1

#9
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1

Excelsius said:

You mean like this:

x=y=(rand() % (RAND_MAX + 1.0));
?

And you're saying that would only work with int? Because I can't use int for a number between 0 and 1
Yeah. If there's a way, I've yet to learn it. I posted some code, you can use that to generate 1000 pseudo-random numbers between 0 and 1.0 (1 decimal precision).
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#10
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
I dunno, I just did this:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>

#define DECIMAL_PLACES 2
#define _STR_PROC(x) # x
#define STRINGIFY(x) _STR_PROC(x)

int main(void)
{
    int multInt = pow(10, DECIMAL_PLACES);
    float multFloat = pow(10.0, DECIMAL_PLACES);
    srand(time(NULL));
    float someNum = (rand() % multInt) / multFloat;
    printf("This number: %." STRINGIFY(DECIMAL_PLACES) "f\n", someNum);
    return 0;
}
The key line is the fourth one in main():
float someNum = (rand() % multInt) / multFloat;
Since you're dividing an int by a float value, the compiler will infer that you want the integer to be "promoted" to a float value, then to perform the division operation. That seems to work for me.
Wow I changed my sig!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users