Extraire d'un fichier.h des variables static ?
Bonjour
je voudrais extraire ranx rany ranz de la fonction rangen() a un moment donne dans mon programme principal.
Pour ce faire j'ai realise void getRanx(unsigned long ran[3]). Malheuresuement au bout de n executions de rangen() j'obtiens :
Citation:
ran[0]=-243427912796356787233568133420600566504719800000000000000000000000000000
00000000000000000000.000000ran[1]=-243427912796647823412860550345535498202352600
00000000000000000000000000000000000000000000000.000000ran[2]=-243427912796025766
59846095574693343295132620000000000000000000000000000000000000000000000000.00000
0
Non seulement les valeurs sont negatives mais en plus elles ne sont pas entre 0 et 1 ...
Voici le code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| #include <math.h>
//double randgen(unsigned long ran[3])
double randgen(void);/* This generates a random number uniformly distributed between 0 and 1 */
int randint(int lo, int hi); /* This generates a random integer uniformly distributed among
the set { lo, lo+1,...., hi-1,hi} */
/* This line initialises the seeds of randgen (it requires three). Change one or more of
the three seed integers to get a new random number sequence.
This line should appear only once in your code. */
static unsigned long ranx=1,rany=1,ranz=1;
/* Randgen returns a double value uniformly distributed between 0 and 1. It uses three integer
seeds (ranx, rany and ranz) */
void getRanx(unsigned long ran[3])
{
ran[0]=ranx;
ran[1]=rany;
ran[2]=ranz;
}
double randgen(void)
{
double register random;
ranx = (171*ranx)%30269;
rany = (172*rany)%30307;
ranz = (170*ranz)%30323;
random = ranx/30269.0+rany/30307.0+ranz/30323.0;
// Discard value (max = 2) to left of decimal point
while(random>1.0)
random -= 1.0;
return random;
} |
Re: Extraire d'un fichier.h des variables static ?
Citation:
Envoyé par Battosaiii
Bonjour
je voudrais extraire ranx rany ranz de la fonction rangen() a un moment donne dans mon programme principal.
Pour ce faire j'ai realise void getRanx(unsigned long ran[3]). Malheuresuement au bout de n executions de rangen() j'obtiens :
Code:
1 2 3 4 5 6
|
ran[0]=-243427912796356787233568133420600566504719800000000000000000000000000000
00000000000000000000.000000ran[1]=-243427912796647823412860550345535498202352600
00000000000000000000000000000000000000000000000.000000ran[2]=-243427912796025766
59846095574693343295132620000000000000000000000000000000000000000000000000.00000
0 |
Probablement un problème de printf(). Ceci fonctionne :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include <math.h>
/* This generates a random number uniformly distributed between 0 and 1 */
double randgen(void);
/* This line initialises the seeds of randgen (it requires three). Change one or more of
the three seed integers to get a new random number sequence.
This line should appear only once in your code. */
static unsigned long ranx = 1, rany = 1, ranz = 1;
/* Randgen returns a double value uniformly distributed between 0 and 1. It uses three integer
seeds (ranx, rany and ranz) */
double randgen(void)
{
double register random;
ranx = (171 * ranx) % 30269;
rany = (172 * rany) % 30307;
ranz = (170 * ranz) % 30323;
random = ranx / 30269.0 + rany / 30307.0 + ranz / 30323.0;
// Discard value (max = 2) to left of decimal point
while (random > 1.0)
random -= 1.0;
return random;
}
#include <stdio.h>
int main (void)
{
int i;
for (i = 0; i < 10; i++)
{
printf ("x = %f\n", randgen());
}
return 0;
} |
Code:
1 2 3 4 5 6 7 8 9 10 11
|
x = 0.016931
x = 0.895254
x = 0.111491
x = 0.939527
x = 0.128230
x = 0.178004
x = 0.299827
x = 0.349718
x = 0.059287
x = 0.821979 |