problem with rand()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pereges

    problem with rand()

    I wrote the following program to generate a seuence of uniformly
    distributed random points between 0 and 1. It is giving only zeros as
    output.
    #include <stdio.h>
    #include <stdlib.h>

    int main(void)
    {

    int i;
    int a;
    for(i=0;i<10;i+ +)
    {
    a =((double)rand( ))/(RAND_MAX+1) ;
    printf("%f", a);
    }
    return 0;
    }





  • Richard Heathfield

    #2
    Re: problem with rand()

    pereges said:
    I wrote the following program to generate a seuence of uniformly
    distributed random points between 0 and 1. It is giving only zeros as
    output.
    #include <stdio.h>
    #include <stdlib.h>
    >
    int main(void)
    {
    >
    int i;
    int a;
    You didn't mean int a, but double a

    #include <stdio.h>
    #include <stdlib.h>

    int main(void)
    {
    int i = 0;
    while(i++ < 10)
    {
    double a = rand() / (RAND_MAX + 1.0);
    printf("%f\n", a);
    }
    return 0;
    }

    Now see if you can work out why you get the same results on every run. (The
    FAQ has something to say about this.)

    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • Joachim Schmitz

      #3
      Re: problem with rand()

      pereges wrote:
      I wrote the following program to generate a seuence of uniformly
      distributed random points between 0 and 1. It is giving only zeros as
      output.
      #include <stdio.h>
      #include <stdlib.h>
      >
      int main(void)
      {
      >
      int i;
      int a;
      double a;
      for(i=0;i<10;i+ +)
      {
      a =((double)rand( ))/(RAND_MAX+1) ;
      printf("%f", a);
      }
      return 0;
      }
      Bye, Jojo


      Comment

      • Lew Pitcher

        #4
        Re: problem with rand()

        pereges wrote:
        I wrote the following program to generate a seuence of uniformly
        distributed random points between 0 and 1. It is giving only zeros as
        output.
        #include <stdio.h>
        #include <stdlib.h>
        >
        int main(void)
        {
        >
        int i;
        int a;
        NB: ---^
        for(i=0;i<10;i+ +)
        {
        a =((double)rand( ))/(RAND_MAX+1) ;
        Questions for the student:
        1) What value of rand() will cause a to equal 1?
        2) How likely would it be for rand() to return the value that would
        cause a to compute out to 1?
        3) What will a be set to for all other values returned by rand()?
        printf("%f", a);
        }
        return 0;
        }
        --
        Lew Pitcher

        Master Codewright & JOAT-in-training | Registered Linux User #112576
        http://pitcher.digitalfreehold.ca/ | GPG public key available by request
        ---------- Slackware - Because I know what I'm doing. ------


        Comment

        • pereges

          #5
          Re: problem with rand()

          On Apr 15, 10:18 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
          pereges said:
          >
          I wrote the following program to generate a seuence of uniformly
          distributed random points between 0 and 1. It is giving only zeros as
          output.
          #include <stdio.h>
          #include <stdlib.h>
          >
          int main(void)
          {
          >
          int i;
          int a;
          >
          You didn't mean int a, but double a
          >
          #include <stdio.h>
          #include <stdlib.h>
          >
          int main(void)
          {
          int i = 0;
          while(i++ < 10)
          {
          double a = rand() / (RAND_MAX + 1.0);
          printf("%f\n", a);
          }
          return 0;
          >
          }
          >
          Now see if you can work out why you get the same results on every run. (The
          FAQ has something to say about this.)
          >
          --
          Richard Heathfield <http://www.cpax.org.uk >
          Email: -http://www. +rjh@
          Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
          "Usenet is a strange place" - dmr 29 July 1999

          Comment

          • CBFalconer

            #6
            Re: problem with rand()

            pereges wrote:
            >
            I wrote the following program to generate a seuence of uniformly
            distributed random points between 0 and 1. It is giving only
            zeros as output.
            >
            #include <stdio.h>
            #include <stdlib.h>
            >
            int main(void) {
            int i;
            int a;
            for(i=0;i<10;i+ +) {
            a =((double)rand( ))/(RAND_MAX+1) ;
            printf("%f", a);
            }
            return 0;
            }
            Putting in proper use of spaces and proper variable typing (note
            the absence of casts) results in:

            #include <stdio.h>
            #include <stdlib.h>

            int main(void) {
            int i;
            double a;

            for (i = 0; i < 10; i++) {
            a = rand() / ((RAND_MAX) + 1.0) ;
            printf("%f\n", a);
            }
            return 0;
            }

            Also note the use of 1.0 in place of 1. This can never generate
            output values of 1.0.

            --
            [mail]: Chuck F (cbfalconer at maineline dot net)
            [page]: <http://cbfalconer.home .att.net>
            Try the download section.

            ** Posted from http://www.teranews.com **

            Comment

            • Jack.Thomson.v3@gmail.com

              #7
              Re: problem with rand()

              On Apr 15, 10:12 pm, pereges <Brol...@gmail. comwrote:
              I wrote the following program to generate a seuence of uniformly
              distributed random points between 0 and 1. It is giving only zeros as
              output.
              #include <stdio.h>
              #include <stdlib.h>
              >
              int main(void)
              {
              >
              int i;
              int a;
              for(i=0;i<10;i+ +)
              {
              a =((double)rand( ))/(RAND_MAX+1) ;
              printf("%f", a);
              }
              return 0;}

              #include <stdio.h>
              #include <stdlib.h>

              int main(void)
              {

              int i;
              double a; // Here is the modification

              for(i=0;i<10;i+ +)
              {
              a =((double)rand( ))/(RAND_MAX+1) ;
              printf("%f", a);
              }
              return 0;
              }


              ~Jack

              Comment

              Working...