# stdin --> random interference --> stdout

• 01-22-2003
j0hnb1ank
stdin --> random interference --> stdout
hey wondering if you can help me

im trying to read in a file which contains 256 double floating point numbers.

im using scanf to do this.

my problem comes when i need to add random values to these numbers, which need to range between +15 and -15, at an interval of around 10-50 samples.

ive heard that 'int rand(void);' included in the library stdlib.h would be useful, but my knowledge of this function is rather limited!

any help would be gratefully appreciated. thank you
• 01-22-2003
Hammer
• 01-22-2003
GanglyLamb
if u do a quick search on thisboard u would find enough examples but anyway

seed rand by using srand (seed it with the time)
then use
Code:

```int random_number; random_number=rand()%15;```
this will give random numbers from 0 to 15

after doing this u could do another rand that will define wether its from +15 to 0 or from 0 to -15
Code:

```int sign; sign=rand()%1;```
+ 2 if statements (to select the sign of the random_number)

This is how i would do it. As im not an advanced programmer this is the only way i can come up with to do this....
• 01-22-2003
Cela
>>this will give random numbers from 0 to 15
Or 0-14 unless your computers' mod math is screwy. :-)

I would get the random number from 0-15 first and then do a split test of another call to rand to see if I should make the number negative or positive.
Code:

```#include <stdlib.h> #include <time.h> int main(void) {   double r;   double f = 123.456;   srand(time(0));   r = rand() % 16;   f += (rand() < RAND_MAX/2) ? -r : r;   printf("%f %f\n", r, f);   return 0; }```