# Thread: need easy help ASAP

1. ## need easy help ASAP

Hi I'm Ali,

My problem is as follows:

*i need to write a program to calculate the INTEGRAL of sin(x), from x=0 to x=pi/2.

*Also, i need to produce 1000 random numbers within 0 and pi/2.

2. show us your best attempt

ssharish2005

3. i'm noob in c prog.

so all i can do is find sin(x) for different values of x. you know, the basic printf and scanf thingy..

i can probably integrate sin(x), but i cant possibly find the integral of sin(x) for x=0 to x=pi/2...

I know pretty much how to generate random numbers, but i cant confine them into a limit (in this case 0 to pi/2).

i'm noob in c prog.

so all i can do is find sin(x) for different values of x. you know, the basic printf and scanf thingy..

i can probably integrate sin(x), but i cant possibly find the integral of sin(x) for x=0 to x=pi/2...

I know pretty much how to generate random numbers, but i cant confine them into a limit (in this case 0 to pi/2).
Maybe try some kind of numerical integration technique like simpson's rule to find the integral of sin(x) from [0 pi/2] .

5. If you're not too picky, you can use rand(), convert to double and reduce into range [0, PI/2].

6. ## random number generator

OK
i found a random number generator program and it goes like this

Code:
```#include <stdio.h>
#include <assert.h>

#define MAXBIT 30

void sobseq(float *x, float *y) {

// Returns two quasi-random numbers for a 2-dimensional Sobel
// sequence. Adapted from Numerical Recipies in C.

int j, k, l;
unsigned long i, im,ipp;

// The following variables are "static" since we want their
// values to remain stored after the function returns. These
// values represent the state of the quasi-random number generator.

static float fac;
static int init=0;
static unsigned long in, ix[3], *iu[2*MAXBIT+1];
static unsigned long mdeg[3]={0,1,2};
static unsigned long ip[3]={0,0,1};
static unsigned long iv[2*MAXBIT+1]=
{0,1,1,1,1,1,1,3,1,3,3,1,1,5,7,7,3,3,5,15,11,5,15,13,9};

// Initialise the generator the first time the function is called.

if (!init) {
init = 1;
for (j = 1, k = 0; j <= MAXBIT; j++, k += 2) iu[j] = &iv[k];
for (k = 1; k <= 2; k++) {
for (j = 1; j <= mdeg[k]; j++) iu[j][k] <<= (MAXBIT-j);
for (j = mdeg[k]+1; j <= MAXBIT; j++) {
ipp = ip[k];
i = iu[j-mdeg[k]][k];
i ^= (i >> mdeg[k]);
for (l = mdeg[k]-1; l >= 1; l--) {
if (ipp & 1) i ^= iu[j-l][k];
ipp >>= 1;
}
iu[j][k] = i;
}
}
fac = 1.0/(1L << MAXBIT);
in = 0;
}

// Now calculate the next pair of numbers in the 2-dimensional
// Sobel sequence.

im = in;
for (j = 1; j <= MAXBIT; j++) {
if (!(im & 1)) break;
im >>= 1;
}
assert(j <= MAXBIT);
im = (j-1)*2;
ix[1] ^= iv[im+1];
ix[2] ^= iv[im+2];
*x = ix[1] * fac;
*y = ix[2] * fac;
in++;
}

int main(void) {

// Test the Sobel generator.

int i;
float x, y;

for (i = 0; i < 1000; i++) {
sobseq(&x, &y);
printf("%f %f\n", x, y);
}
getchar();
getchar();
return 0;
}```

but i still need to know how to limit the random number being generated to between 0 and Pi/2.

BTW... does anybody know how to use monte carlo technique for c programming??

I need it for my assignment..

7. Originally Posted by jafet
If you're not too picky, you can use rand(), convert to double and reduce into range [0, PI/2].
Can u please give me a simple example because i dunno how to use rand() or double...

8. Q: What does rand() return ?
A: An int between 0 and RAND_MAX.
Q:How can I convert an int to a double ?
A: Cast.
Q: How can I change the range ?
A: Divide by original range and multiply the result with the new range.
Kurt

9. Originally Posted by ZuK
Q: What does rand() return ?
A: An int between 0 and RAND_MAX.
Q:How can I convert an int to a double ?
A: Cast.
Q: How can I change the range ?
A: Divide by original range and multiply the result with the new range.
Kurt
I've just written a simple program which returns 1 random number...

BUT WHY DOES IT ALWAYS RETURN AROUND 27000!!!???

is there a trick to fix this?

also, can you show me how to limit the range of the random number from 0 to pi/2?
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
int i;

srand(time(NULL));

i = rand();

printf ("Your random number is %d\n", i);

printf ("This compiler can generate random numbers from 0 to %d\n", RAND_MAX);

return(0);
}```

10. double is basically a float, but with more precision and a higher range. It's usually implemented as a double word in a computer.

rand() returns a pseudorandom number using a built-in algorithm. It is always seeded with 1 on program startup. You can re-seed it with anytime by passing an int to srand().

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

int i = rand(); //returns a random number between 1 and RAND_MAX
printf("%d", i); //prints something like "12345"
double d = i / RAND_MAX * PI; //something like 1.4blahblahblah```
By the way, "//" comments weren't defined in C until C99, IIRC.

11. ## Why doesn't this work??!!

Code:
```#include <stdlib.h>
int min(void){

int i;

i= rand();

printf("%d", i);

double d = i / RAND_MAX * PI;
return 0;
}```

12. Code:
```X = ((double)rand() / ((double)(RAND_MAX)+(double)(1)) );
printf("%f\n",X,X*(PI)/2));```
ssharish2005

13. > Why doesn't this work??!!
You didn't call srand() before calling rand()
You don't have any print statements after your calculation.

14. Code:
`int min(void){`
typo?

15. Originally Posted by ssharish2005
Code:
```X = ((double)rand() / ((double)(RAND_MAX)+(double)(1)) );
printf("%f\n",X,X*(PI)/2));```
ssharish2005
What's the deal with all those casts?
Code:
`X = (double)rand() / (RAND_MAX + 1);`
As for that integral. Correct me if I'm wrong, but for the given task this should suffice:
Code:
`double definite_integral = -cos(PI / 2.0) - (-cos(0.0));`