Redirect to STDOUT Problem

Discussion in 'C Programming' started by Jordan Glassman, May 13, 2008.

  1. Trying to do something fairly routine... drop output into a file to
    graph, but the following command at the bash command line:

    ising > output

    produces a blinking cursor, an empty file named "output," and a
    program that runs forever. With no redirect, the output is normal.
    Below is the printf statement used to generate output.

    printf("B=%4.2f E=%8.4f m=%8.4f Cv=%5.2f \
    Chi=%5.2f\n",beta,(energy/n2),(moment/n2), \
    (summe2-summe*summe)/n2, \
    (summm2-summm*summm)/n2);

    I verified that things like 'ls > output' work fine, and that I can
    also redirect output from other c programs I've written. Something is
    funny about this one.

    Any thoughts?
    Jordan Glassman, May 13, 2008
    #1
    1. Advertising

  2. Jordan Glassman <> writes:

    > Trying to do something fairly routine... drop output into a file to
    > graph, but the following command at the bash command line:
    >
    > ising > output


    On modern systems, . is often not in your path. Did you just forget
    to type ./ising?

    > produces a blinking cursor, an empty file named "output," and a
    > program that runs forever. With no redirect, the output is normal.
    > Below is the printf statement used to generate output.
    >
    > printf("B=%4.2f E=%8.4f m=%8.4f Cv=%5.2f \
    > Chi=%5.2f\n",beta,(energy/n2),(moment/n2), \
    > (summe2-summe*summe)/n2, \
    > (summm2-summm*summm)/n2);


    Nothing here can explain what is happening. The printf is correct.
    Of course, if n2 == 0 you won't get any output but that will happen
    with and without the redirect.

    > I verified that things like 'ls > output' work fine, and that I can
    > also redirect output from other c programs I've written. Something is
    > funny about this one.
    >
    > Any thoughts?


    The problem is probably not in the C code. You need someone to watch
    exactly what you are doing and they'll probably spot it in no time. I
    think a group like comp.unix.programmer might be better.

    --
    Ben.
    Ben Bacarisse, May 13, 2008
    #2
    1. Advertising

  3. Ben,

    Thanks for the reply...

    > On modern systems, . is often not in your path. Did you just forget
    > to type ./ising?


    No, that's not the problem.

    > Nothing here can explain what is happening. The printf is correct.
    > Of course, if n2 == 0 you won't get any output but that will happen
    > with and without the redirect.
    >
    > The problem is probably not in the C code. You need someone to watch
    > exactly what you are doing and they'll probably spot it in no time. I
    > think a group like comp.unix.programmer might be better.


    I tend to agree, but since *everything else* works except this
    particular c program, which contains nothing more than a few
    calculations inside loops and this one output statement, I decided to
    post here first.

    -j
    Jordan Glassman, May 13, 2008
    #3
  4. Jordan Glassman wrote:
    > ...
    > I tend to agree, but since *everything else* works except this
    > particular c program, which contains nothing more than a few
    > calculations inside loops and this one output statement, I
    > decided to post here first.


    Though for some reason you decided not to post the code.
    Still, your loss...

    --
    Peter
    Peter Nilsson, May 13, 2008
    #4
  5. >
    > Though for some reason you decided not to post the code.
    > Still, your loss...
    >


    My loss? Here it is...

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    #include <curses.h>

    #include <unistd.h> // ANSI C???

    #define N 40
    #define NITERS 5000000

    void filllattice(int (*m)[N]);
    void seed(void); /* bad bad bad */
    void printlattice(int (*m)[N]);
    void ising(int (*m)[N],double beta);

    int main(){

    int lattice[N][N];
    double beta=0.0;

    seed();

    for(beta=0.5;beta<=1.2;beta+=0.01){

    filllattice(lattice);

    // clear();

    // printlattice(lattice);

    ising(lattice,beta);

    }

    return 0;

    }

    void filllattice(int (*m)[N]){

    int i,j;

    for(i=0;i<N;i++){
    for(j=0;j<N;j++){
    if((((float)rand()/(float)RAND_MAX)) > 0.5) m[j]= 1;
    else m[j] = -1;
    }
    }

    }

    void seed(void){

    struct tm *mt;
    int i;
    time_t t;
    char timestr[3];

    t=time(NULL);
    mt=localtime(&t);
    strftime(timestr,3,"%S",mt);

    srand(atoi(timestr));

    }

    void printlattice(int (*m)[N]){

    int i,j;

    /* system("clear"); */
    move(0,0);

    for(i=0;i<N;i++){
    putchar('\n');
    for(j=0;j<N;j++){
    if(m[j]==1)printf(" 1");
    else printf(" -1");
    }
    }

    putchar('\n');
    }

    void ising(int (*m)[N],double beta){

    int i,j,k;
    double energy,moment;
    double summe,summm,summe2,summm2;
    int numsteps=0;
    double sum_nn,sums;
    double deltaH;
    double sum_nnn;
    int n2=N*N;
    double half=NITERS/2;

    k=0;

    energy=moment=0.0;
    summe=summm=summe2=summm2=0.0;

    // pick a site randomly

    for(i=0;i<N;i++){
    for(j=0;j<N;j++){
    moment += m[j];
    energy -= 2*m[j]*(m[(i+1)%N][j]+m[(j+1)%N]+m[(i+1)%N][(j
    +1)%N]/5);
    }
    }

    for(k=0;k<NITERS;k++){

    i=rand()%N;
    j=rand()%N;

    sum_nn=m[((j+1)%N)]+m[((j-1+N)%N)]+m[((i+1)%N)][j]+m[((i-1+N)
    %N)][j];
    sum_nnn=(m[((i+1)%N)][((j+1)%N)]+m[((i-1+N)%N)][((j-1+N)%N)]
    +m[((i-1+N)%N)][((j+1)%N)]+m[((i+1)%N)][((j-1+N)%N)])/5;
    sums=sum_nn+sum_nnn;
    deltaH=m[j]*sums;
    if((exp(-1.3*deltaH*beta))>((float)rand()/(float)RAND_MAX) ||
    (deltaH < 0) ){
    energy += 2*deltaH;
    moment -= 2*m[j];
    m[j] *= -1;
    }

    if(k>half){
    summe+=energy;
    summe2+=energy*energy;
    summm+=moment;
    summm2+=moment*moment;
    }

    /* if(k>10000){
    k=0;
    putchar('\n');
    printf("E=%f m=%f",(energy/n2),(moment/n2));
    printlattice(m);
    sleep(1);
    } */

    }

    summe /= half;
    summe2 /= half;
    summm /= half;
    summm2 /= half;

    printf("B=%4.2f E=%8.4f m=%8.4f Cv=%5.2f Chi=%5.2f\n",beta,(energy/
    n2),(moment/n2),(summe2-summe*summe)/n2,(summm2-summm*summm)/n2);

    }
    Jordan Glassman, May 13, 2008
    #5
  6. Jordan Glassman <> writes:

    >> Though for some reason you decided not to post the code.
    >> Still, your loss...
    >>

    >
    > My loss? Here it is...


    Yes. I can now tell you how to get it to work (but
    comp.unix.programmer is still the better place).

    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <time.h>
    > #include <math.h>
    > #include <curses.h>


    curses programs can't usually have their put redirected (ask in
    c.u.p for when and why).

    > #include <unistd.h> // ANSI C???


    No, not ANSI C.

    <snip>
    > void seed(void){
    >
    > struct tm *mt;
    > int i;
    > time_t t;
    > char timestr[3];
    >
    > t=time(NULL);
    > mt=localtime(&t);
    > strftime(timestr,3,"%S",mt);
    >
    > srand(atoi(timestr));
    >
    > }


    The comp.lang.c FAQ has some advice seeding rand().
    http://c-faq.com/lib/srand.html

    > void printlattice(int (*m)[N]){
    >
    > int i,j;
    >
    > /* system("clear"); */
    > move(0,0);


    Comment this out. You don't need the curses functionality (at least
    not yet). If you do more with it in future versions, it may be worth
    the price of losing output re-direction.

    > for(i=0;i<N;i++){
    > putchar('\n');
    > for(j=0;j<N;j++){
    > if(m[j]==1)printf(" 1");
    > else printf(" -1");
    > }
    > }
    >
    > putchar('\n');
    > }
    >
    > void ising(int (*m)[N],double beta){
    >
    > int i,j,k;
    > double energy,moment;
    > double summe,summm,summe2,summm2;
    > int numsteps=0;
    > double sum_nn,sums;
    > double deltaH;
    > double sum_nnn;
    > int n2=N*N;
    > double half=NITERS/2;
    >
    > k=0;
    >
    > energy=moment=0.0;
    > summe=summm=summe2=summm2=0.0;
    >
    > // pick a site randomly
    >
    > for(i=0;i<N;i++){
    > for(j=0;j<N;j++){
    > moment += m[j];
    > energy -= 2*m[j]*(m[(i+1)%N][j]+m[(j+1)%N]+m[(i+1)%N][(j
    > +1)%N]/5);
    > }
    > }
    >
    > for(k=0;k<NITERS;k++){
    >
    > i=rand()%N;
    > j=rand()%N;
    >
    > sum_nn=m[((j+1)%N)]+m[((j-1+N)%N)]+m[((i+1)%N)][j]+m[((i-1+N)
    > %N)][j];
    > sum_nnn=(m[((i+1)%N)][((j+1)%N)]+m[((i-1+N)%N)][((j-1+N)%N)]
    > +m[((i-1+N)%N)][((j+1)%N)]+m[((i+1)%N)][((j-1+N)%N)])/5;
    > sums=sum_nn+sum_nnn;
    > deltaH=m[j]*sums;
    > if((exp(-1.3*deltaH*beta))>((float)rand()/(float)RAND_MAX) ||
    > (deltaH < 0) ){
    > energy += 2*deltaH;
    > moment -= 2*m[j];
    > m[j] *= -1;
    > }
    >
    > if(k>half){
    > summe+=energy;
    > summe2+=energy*energy;
    > summm+=moment;
    > summm2+=moment*moment;
    > }
    >
    > /* if(k>10000){
    > k=0;
    > putchar('\n');
    > printf("E=%f m=%f",(energy/n2),(moment/n2));
    > printlattice(m);
    > sleep(1);
    > } */
    >
    > }
    >
    > summe /= half;
    > summe2 /= half;
    > summm /= half;
    > summm2 /= half;
    >
    > printf("B=%4.2f E=%8.4f m=%8.4f Cv=%5.2f Chi=%5.2f\n",beta,(energy/
    > n2),(moment/n2),(summe2-summe*summe)/n2,(summm2-summm*summm)/n2);


    Add:

    fflush(stdout);

    to be sure to get what data you have written so far. I did not wait for
    the program to finish -- if you do there is no need to call fflush.

    --
    Ben.
    Ben Bacarisse, May 13, 2008
    #6
  7. Ben Bacarisse <> writes:

    > curses programs can't usually have their put redirected (ask in
    > c.u.p for when and why).


    Oh the perils of off-topic answers. Please don't get into this here.
    I know it is wrong -- I was over-simplifying to the point of being
    plain wrong. Berate me in comp.unix.programmer if you must!

    --
    Ben.
    Ben Bacarisse, May 13, 2008
    #7
  8. [Other replies notwithstanding...]

    Jordan Glassman wrote:
    > #define NITERS 5000000
    > int i,j,k;
    > for(k=0;k<NITERS;k++){


    Are you perchance running on a machine with 16-bit ints?

    If so, this loop will likely run forever (assuming 16-bit overflow
    from INT_MAX to INT_MIN.)

    > void ising(int (*m)[N],double beta){


    Identifiers starting with is (or to) and a lowercase letter are
    reserved for use as external identifiers.

    This is not likely to be the source of your problem, but it is
    still a fault with your code.

    Apart from that, the program does take some time to run.
    Are you leaving it for long enough? With such relatively
    small output, many systems won't actually flush to the
    redirected file until the program (and stream) closes.

    --
    Peter
    Peter Nilsson, May 13, 2008
    #8
  9. Ben,

    Taking out the curses.h and all associated functions did it. I'm
    still not sure why the printf didn't work, though... not of the
    curses.h functions were actually being used in that code I posted...
    they were all commented out! It's as if curses replaces printf with
    another version.

    Dunno. In any case, thanks for the help.

    -j

    On May 12, 9:01 pm, Ben Bacarisse <> wrote:
    > Jordan Glassman <> writes:
    > >> Though for some reason you decided not to post the code.
    > >> Still, your loss...

    >
    > > My loss? Here it is...

    >
    > Yes. I can now tell you how to get it to work (but
    > comp.unix.programmer is still the better place).
    >
    > > #include <stdio.h>
    > > #include <stdlib.h>
    > > #include <time.h>
    > > #include <math.h>
    > > #include <curses.h>

    >
    > curses programs can't usually have their put redirected (ask in
    > c.u.p for when and why).
    >
    > > #include <unistd.h> // ANSI C???

    >
    > No, not ANSI C.
    >
    > <snip>
    >
    > > void seed(void){

    >
    > > struct tm *mt;
    > > int i;
    > > time_t t;
    > > char timestr[3];

    >
    > > t=time(NULL);
    > > mt=localtime(&t);
    > > strftime(timestr,3,"%S",mt);

    >
    > > srand(atoi(timestr));

    >
    > > }

    >
    > The comp.lang.c FAQ has some advice seeding rand().http://c-faq.com/lib/srand.html
    >
    > > void printlattice(int (*m)[N]){

    >
    > > int i,j;

    >
    > > /* system("clear"); */
    > > move(0,0);

    >
    > Comment this out. You don't need the curses functionality (at least
    > not yet). If you do more with it in future versions, it may be worth
    > the price of losing output re-direction.
    >
    >
    >
    > > for(i=0;i<N;i++){
    > > putchar('\n');
    > > for(j=0;j<N;j++){
    > > if(m[j]==1)printf(" 1");
    > > else printf(" -1");
    > > }
    > > }

    >
    > > putchar('\n');
    > > }

    >
    > > void ising(int (*m)[N],double beta){

    >
    > > int i,j,k;
    > > double energy,moment;
    > > double summe,summm,summe2,summm2;
    > > int numsteps=0;
    > > double sum_nn,sums;
    > > double deltaH;
    > > double sum_nnn;
    > > int n2=N*N;
    > > double half=NITERS/2;

    >
    > > k=0;

    >
    > > energy=moment=0.0;
    > > summe=summm=summe2=summm2=0.0;

    >
    > > // pick a site randomly

    >
    > > for(i=0;i<N;i++){
    > > for(j=0;j<N;j++){
    > > moment += m[j];
    > > energy -= 2*m[j]*(m[(i+1)%N][j]+m[(j+1)%N]+m[(i+1)%N][(j
    > > +1)%N]/5);
    > > }
    > > }

    >
    > > for(k=0;k<NITERS;k++){

    >
    > > i=rand()%N;
    > > j=rand()%N;

    >
    > > sum_nn=m[((j+1)%N)]+m[((j-1+N)%N)]+m[((i+1)%N)][j]+m[((i-1+N)
    > > %N)][j];
    > > sum_nnn=(m[((i+1)%N)][((j+1)%N)]+m[((i-1+N)%N)][((j-1+N)%N)]
    > > +m[((i-1+N)%N)][((j+1)%N)]+m[((i+1)%N)][((j-1+N)%N)])/5;
    > > sums=sum_nn+sum_nnn;
    > > deltaH=m[j]*sums;
    > > if((exp(-1.3*deltaH*beta))>((float)rand()/(float)RAND_MAX) ||
    > > (deltaH < 0) ){
    > > energy += 2*deltaH;
    > > moment -= 2*m[j];
    > > m[j] *= -1;
    > > }

    >
    > > if(k>half){
    > > summe+=energy;
    > > summe2+=energy*energy;
    > > summm+=moment;
    > > summm2+=moment*moment;
    > > }

    >
    > > /* if(k>10000){
    > > k=0;
    > > putchar('\n');
    > > printf("E=%f m=%f",(energy/n2),(moment/n2));
    > > printlattice(m);
    > > sleep(1);
    > > } */

    >
    > > }

    >
    > > summe /= half;
    > > summe2 /= half;
    > > summm /= half;
    > > summm2 /= half;

    >
    > > printf("B=%4.2f E=%8.4f m=%8.4f Cv=%5.2f Chi=%5.2f\n",beta,(energy/
    > > n2),(moment/n2),(summe2-summe*summe)/n2,(summm2-summm*summm)/n2);

    >
    > Add:
    >
    > fflush(stdout);
    >
    > to be sure to get what data you have written so far. I did not wait for
    > the program to finish -- if you do there is no need to call fflush.
    >
    > --
    > Ben.
    Jordan Glassman, May 13, 2008
    #9
  10. Ben,

    Taking out the curses.h and all associated functions did it. I'm
    still not sure why the printf didn't work, though... not of the
    curses.h functions were actually being used in that code I posted...
    they were all commented out! It's as if curses replaces printf with
    another version.

    Dunno. In any case, thanks for the help.

    -j

    On May 12, 9:01 pm, Ben Bacarisse <> wrote:
    > Jordan Glassman <> writes:
    > >> Though for some reason you decided not to post the code.
    > >> Still, your loss...

    >
    > > My loss? Here it is...

    >
    > Yes. I can now tell you how to get it to work (but
    > comp.unix.programmer is still the better place).
    >
    > > #include <stdio.h>
    > > #include <stdlib.h>
    > > #include <time.h>
    > > #include <math.h>
    > > #include <curses.h>

    >
    > curses programs can't usually have their put redirected (ask in
    > c.u.p for when and why).
    >
    > > #include <unistd.h> // ANSI C???

    >
    > No, not ANSI C.
    >
    > <snip>
    >
    > > void seed(void){

    >
    > > struct tm *mt;
    > > int i;
    > > time_t t;
    > > char timestr[3];

    >
    > > t=time(NULL);
    > > mt=localtime(&t);
    > > strftime(timestr,3,"%S",mt);

    >
    > > srand(atoi(timestr));

    >
    > > }

    >
    > The comp.lang.c FAQ has some advice seeding rand().http://c-faq.com/lib/srand.html
    >
    > > void printlattice(int (*m)[N]){

    >
    > > int i,j;

    >
    > > /* system("clear"); */
    > > move(0,0);

    >
    > Comment this out. You don't need the curses functionality (at least
    > not yet). If you do more with it in future versions, it may be worth
    > the price of losing output re-direction.
    >
    >
    >
    > > for(i=0;i<N;i++){
    > > putchar('\n');
    > > for(j=0;j<N;j++){
    > > if(m[j]==1)printf(" 1");
    > > else printf(" -1");
    > > }
    > > }

    >
    > > putchar('\n');
    > > }

    >
    > > void ising(int (*m)[N],double beta){

    >
    > > int i,j,k;
    > > double energy,moment;
    > > double summe,summm,summe2,summm2;
    > > int numsteps=0;
    > > double sum_nn,sums;
    > > double deltaH;
    > > double sum_nnn;
    > > int n2=N*N;
    > > double half=NITERS/2;

    >
    > > k=0;

    >
    > > energy=moment=0.0;
    > > summe=summm=summe2=summm2=0.0;

    >
    > > // pick a site randomly

    >
    > > for(i=0;i<N;i++){
    > > for(j=0;j<N;j++){
    > > moment += m[j];
    > > energy -= 2*m[j]*(m[(i+1)%N][j]+m[(j+1)%N]+m[(i+1)%N][(j
    > > +1)%N]/5);
    > > }
    > > }

    >
    > > for(k=0;k<NITERS;k++){

    >
    > > i=rand()%N;
    > > j=rand()%N;

    >
    > > sum_nn=m[((j+1)%N)]+m[((j-1+N)%N)]+m[((i+1)%N)][j]+m[((i-1+N)
    > > %N)][j];
    > > sum_nnn=(m[((i+1)%N)][((j+1)%N)]+m[((i-1+N)%N)][((j-1+N)%N)]
    > > +m[((i-1+N)%N)][((j+1)%N)]+m[((i+1)%N)][((j-1+N)%N)])/5;
    > > sums=sum_nn+sum_nnn;
    > > deltaH=m[j]*sums;
    > > if((exp(-1.3*deltaH*beta))>((float)rand()/(float)RAND_MAX) ||
    > > (deltaH < 0) ){
    > > energy += 2*deltaH;
    > > moment -= 2*m[j];
    > > m[j] *= -1;
    > > }

    >
    > > if(k>half){
    > > summe+=energy;
    > > summe2+=energy*energy;
    > > summm+=moment;
    > > summm2+=moment*moment;
    > > }

    >
    > > /* if(k>10000){
    > > k=0;
    > > putchar('\n');
    > > printf("E=%f m=%f",(energy/n2),(moment/n2));
    > > printlattice(m);
    > > sleep(1);
    > > } */

    >
    > > }

    >
    > > summe /= half;
    > > summe2 /= half;
    > > summm /= half;
    > > summm2 /= half;

    >
    > > printf("B=%4.2f E=%8.4f m=%8.4f Cv=%5.2f Chi=%5.2f\n",beta,(energy/
    > > n2),(moment/n2),(summe2-summe*summe)/n2,(summm2-summm*summm)/n2);

    >
    > Add:
    >
    > fflush(stdout);
    >
    > to be sure to get what data you have written so far. I did not wait for
    > the program to finish -- if you do there is no need to call fflush.
    >
    > --
    > Ben.
    Jordan Glassman, May 13, 2008
    #10
  11. Jordan Glassman

    Default User Guest

    Re: Redirect to STDOUT Problem - TPA

    Jordan Glassman wrote:

    > Ben,



    Please don't top-post. Your replies belong following or interspersed
    with properly trimmed quotes. See the majority of other posts in the
    newsgroup, or:
    <http://www.caliburn.nl/topposting.html>
    Default User, May 13, 2008
    #11
  12. Re: Redirect to STDOUT Problem - TPA

    > Please don't top-post.

    Lesson learned...
    Jordan Glassman, May 13, 2008
    #12
  13. Jordan Glassman

    Default User Guest

    Re: Redirect to STDOUT Problem - TPA

    Jordan Glassman wrote:

    > > Please don't top-post.

    >
    > Lesson learned...


    Thanks!




    Brian
    Default User, May 13, 2008
    #13
  14. Jordan Glassman

    viza Guest

    On May 13, 5:03 am, Richard Heathfield <> wrote:
    > Jordan Glassman said:


    > > if((((float)rand()/(float)RAND_MAX)) > 0.5)

    >
    > In C, the normal way to do (pseudo) random numbers selected from N
    > possibilities (in the range 0 to N-1) is to get a value in the half-open
    > range [0, 1) like this:
    >
    > rand() / (RAND_MAX + 1.0)
    >
    > and then multiply by N to get a value in the half-open range [0, N).
    > It seems that you want a value in the half-open range [0, 1) so you can
    > omit the multiplication step:
    >
    > if(rand() / (RAND_MAX + 1.0) > 0.5)


    In this case, why not just use:

    if( rand() & 1 )

    If RAND_MAX is odd (which it usually is) then this is perfect, and
    doesn't need an FPU.

    If RAND_MAX is even, then it gets very close to perfect as long as
    RAND_MAX is large. Remember that the float method isn't perfect
    either.

    If your rand() isn't evenly distributed, then you are screwed whatever
    you do.

    viza
    viza, May 13, 2008
    #14
  15. In article <>,
    viza <> wrote:
    >On May 13, 5:03 am, Richard Heathfield <> wrote:
    >> Jordan Glassman said:


    >> > if((((float)rand()/(float)RAND_MAX)) > 0.5)


    >In this case, why not just use:


    >if( rand() & 1 )


    >If RAND_MAX is odd (which it usually is) then this is perfect, and
    >doesn't need an FPU.


    The low bit of rand() is often not very random at all -- often alternating
    between 0 and 1 each time. It's a problem with linear congruential
    pseudo-random numbers, at least if you take the bottom bits of the
    new seed as being the random value. (Some implementations make sure
    they never output the bottom bits to reduce this problem, but
    C doesn't make any such promises.)
    --
    "Man's life is but a jest,
    A dream, a shadow, bubble, air, a vapor at the best."
    -- George Walter Thornbury
    Walter Roberson, May 13, 2008
    #15
  16. In article <g0cuqm$ael$>,
    Walter Roberson <-cnrc.gc.ca> wrote:

    >The low bit of rand() is often not very random at all -- often alternating
    >between 0 and 1 each time.


    This used to be true, 20-something years ago. Surely modern
    implementations don't have the same problem?

    -- Richard
    --
    :wq
    Richard Tobin, May 13, 2008
    #16
  17. In article <g0d2i5$26j0$>,
    Richard Tobin <> wrote:
    >In article <g0cuqm$ael$>,
    >Walter Roberson <-cnrc.gc.ca> wrote:


    >>The low bit of rand() is often not very random at all -- often alternating
    >>between 0 and 1 each time.


    >This used to be true, 20-something years ago. Surely modern
    >implementations don't have the same problem?


    Remember, the C89 standard was 20-something years ago, but conforming
    more modern implementations (C99) are still pretty uncommon ;-)

    To be fair: the C89 sample implementation works with an
    unsigned long int seed, and the value returned is divided by 64K and
    is mod 32768 (e.g., shift right 16 bits and mask out all but the
    bottom 15 bits). With that sample algorithm, the bottom bit of rand()
    should have a cycle of 64K (which would, I -suspect-, be composed
    of two half-cycles of 32K each, bitwise negations of each other.)
    --
    "The shallow murmur, but the deep are dumb." -- Sir Walter Raleigh
    Walter Roberson, May 13, 2008
    #17
  18. Jordan Glassman

    Guest

    Richard Tobin <> wrote:
    > In article <g0cuqm$ael$>,
    > Walter Roberson <-cnrc.gc.ca> wrote:
    >
    >>The low bit of rand() is often not very random at all -- often alternating
    >>between 0 and 1 each time.

    >
    > This used to be true, 20-something years ago. Surely modern
    > implementations don't have the same problem?


    I only know of *one* implementation that had that problem, and that was
    more like 30 years ago. (Someone at UCB "enhanced" a perfectly good
    16-bit rand() to return 32 bit values by simply returning the entire
    internal state rather than just the high-order 16 bits.)

    -- Larry Jones

    I wonder what's on TV now. -- Calvin
    , May 14, 2008
    #18
  19. > produces a blinking cursor, an empty file named "output," and a
    > program that runs forever.  With no redirect, the output is normal.


    As I see your problem has been solved. I got similar behaviour
    when I did not wait for the program to completely finish.
    Without redirection I saw the results immediately, but
    redirection sometimes implies a buffering and the buffer
    is lost if the program terminates abnormally.

    > Below is the printf statement used to generate output.
    >
    > printf("B=%4.2f E=%8.4f m=%8.4f Cv=%5.2f \
    > Chi=%5.2f\n",beta,(energy/n2),(moment/n2), \
    > (summe2-summe*summe)/n2, \
    > (summm2-summm*summm)/n2);


    Sorry for this remark, but, this last lines made me
    suspicious: This is a 2dimensional ising model,
    Cv is the specific heat, which is the energy density fluctuation,
    so I expect a standard deviation there.
    Don't you then miss an other 1/n2 no normalise summe and summm?
    I can be completely wrong, but you might want to check this.

    Szabolcs
    Szabolcs Borsanyi, May 14, 2008
    #19
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VisionSet
    Replies:
    6
    Views:
    9,611
    Sudsy
    Sep 20, 2003
  2. Elad
    Replies:
    0
    Views:
    397
  3. Sal
    Replies:
    1
    Views:
    374
  4. Andreas S
    Replies:
    3
    Views:
    243
    Eric Hodel
    Dec 9, 2006
  5. Replies:
    2
    Views:
    319
    A. Sinan Unur
    Dec 7, 2005
Loading...

Share This Page