|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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; } |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. ------ |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On Apr 15, 10:18 pm, Richard Heathfield <r...@see.sig.invalid> wrote:
> 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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 ** |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
On Apr 15, 10:12 pm, pereges <Brol...@gmail.com> 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;} #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 http://programmingsite.googlepages.com |
|
![]() |
| Outils de la discussion | |
|
|