PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > problem with rand()
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
problem with rand()

Réponse
 
LinkBack Outils de la discussion
Vieux 15/04/2008, 18h12   #1
pereges
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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;
}





  Réponse avec citation
Vieux 15/04/2008, 18h14   #2
Joachim Schmitz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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


  Réponse avec citation
Vieux 15/04/2008, 18h18   #3
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 15/04/2008, 18h23   #4
Lew Pitcher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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. ------


  Réponse avec citation
Vieux 15/04/2008, 19h13   #5
pereges
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: problem with rand()

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


  Réponse avec citation
Vieux 15/04/2008, 22h35   #6
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 **
  Réponse avec citation
Vieux 16/04/2008, 08h30   #7
Jack.Thomson.v3@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: problem with rand()

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 13h50.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,19424 seconds with 15 queries