hilfe bitte warum erzeugt rand() immer bei die variablen 1??

Gast #6035088
Lesenswert?

void generatedata(wetterstation mydata[], int size)
{
  for (int i = 0; i < size; i++)
  {
    mydata[i].temperatur = (float) (rand()% 410 + (-200) * 0,1);
    mydata[i].luftfeuchtigkeit = (float) (rand()% (1010 + 0)*0,1);
    mydata[i].luftdruck = (float) (rand()% (11010 + 9000) * 0,1);
    mydata[i].messzeit = GetTickCount();

    printf("messzeit:%d\n\tluftdruck:%f\n\tluftfeuchtigkeit:%f\n\ttemperatur 
:%f\n\t",  mydata[i].messzeit, mydata[i].luftdruck, 
mydata[i].luftfeuchtigkeit, mydata[i].temperatur);

  }

}
#6035130
Lesenswert?

So in etwa sollte das eher tun:
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <time.h>
4

5
typedef struct {
6
    float temperatur;
7
    float luftfeuchtigkeit;
8
    float luftdruck;
9
    int messzeit;
10
} wetterstation;
11

12
void generatedata(wetterstation mydata[], int size)
13
{
14
  for (int i = 0; i < size; i++)
15
  {
16
    mydata[i].temperatur = (float) (rand()% 410 + (-200) * 0.1);
17
    mydata[i].luftfeuchtigkeit = (float) (rand()% (1010 + 0) * 0.1);
18
    mydata[i].luftdruck = (float) (rand()% (11010 + 9000) * 0.1);
19
    mydata[i].messzeit = 1; // GetTickCount();
20

21
    printf("messzeit: %d\nluftdruck: %f\nluftfeuchtigkeit: %f\ntemperatur: %f\n",  
22
    mydata[i].messzeit, mydata[i].luftdruck, mydata[i].luftfeuchtigkeit, mydata[i].temperatur);
23
  }
24

25
}
26

27
int main()
28
{
29
    srand(time(NULL)); // use current time as seed for random generator
30
    wetterstation w1[2];
31
    generatedata(w1, 2);
32
    
33
    return 0;
34
}
Gast #6035242
Lesenswert?

wewerica schrieb:
> mydata[i].temperatur = (float) (rand()% 410 + (-200) * 0,1);

Mark B. schrieb:
> mydata[i].temperatur = (float) (rand()% 410 + (-200) * 0.1);

auf die Klammern achten..
das da ist gleich (rand()%410) - 20
und nicht etwa wie vermutlich gewünscht (rand()%410 - 200) *0.1

ausgehend von
wewerica schrieb:
> mydata[i].luftfeuchtigkeit = (float) (rand()% (1010 + 0)*0,1);

Mark B. schrieb:
> mydata[i].luftfeuchtigkeit = (float) (rand()% (1010 + 0) * 0.1);

das da ist (rand()%1010) * 0.1

und
wewerica schrieb:
> mydata[i].luftdruck = (float) (rand()% (11010 + 9000) * 0,1);

Mark B. schrieb:
> mydata[i].luftdruck = (float) (rand()% (11010 + 9000) * 0.1);

welches ja (rand()%20010) * 0.1 wäre.
Beitrag #6035262 wurde von einem Moderator gelöscht.

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren