stdin --> random interference --> stdout

This is a discussion on stdin --> random interference --> stdout within the C Programming forums, part of the General Programming Boards category; hey wondering if you can help me im trying to read in a file which contains 256 double floating point ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    2

    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

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    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....

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>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;
    }
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Replies: 2
    Last Post: 07-12-2008, 12:00 PM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 04:51 PM
  4. STDIN stdout
    By GreyMattr in forum Linux Programming
    Replies: 2
    Last Post: 08-01-2002, 01:29 PM
  5. What data type are stdout and stdin?
    By KwikDrawMcGraw in forum C Programming
    Replies: 1
    Last Post: 07-16-2002, 01:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21