# weather

• JTP
• Topic Author
• Offline
• Platinum Member
3 years 3 weeks ago #7951 by JTP
weather was created by JTP
Trying to give the weather more variety:

```    case 22:
weather_info.sunlight = SUN_DARK;
if (rand_number(0, 1) < 1) {
send_to_outdoor("The night has begun.\r\n");
} else {
send_to_outdoor("The night has begun, ......\r\n");
}
break;```
but i seem to keep getting the else message every time ?

3 years 3 weeks ago - 3 years 3 weeks ago #7952 by Krell
Replied by Krell on topic weather
rand_number() in utils.c ends with the following line
`return ((circle_random() % (to - from + 1)) + from);`

Modulo returns the remainder of division. If you're dividing by zero you're going to get an error, are you not? It would seem intuitive that the remainder of dividing any number by zero would be zero, except for that whole error thing. Perhaps this is why you always end up with the 'else' option, because the system can't calculate the first?

Perhaps a real programmer can comment.
Last edit: 3 years 3 weeks ago by Krell.

• JTP
• Topic Author
• Offline
• Platinum Member
3 years 3 weeks ago #7953 by JTP
Replied by JTP on topic weather
Just tried 1, 2 and <2

Now i keep getting the first message instead.

• JTP
• Topic Author
• Offline
• Platinum Member
3 years 3 weeks ago #7954 by JTP
Replied by JTP on topic weather
So might be a bracket problem, i never Can Seem to get em right.

3 years 3 weeks ago - 3 years 3 weeks ago #7955 by Krell
Replied by Krell on topic weather
Actually, my original answer was a bit off too. I mixed up "to" and "from". Still, you're doing Modulo, so you have to consider the remainder right? If the Modulo of circle_random() is less than one or two, depending on your condition, then it'll be the first statement executed, otherwise the second statement. I think Modulo is usually performed in integer maths too, IIRC. I don't if any of this helps.
Last edit: 3 years 3 weeks ago by Krell.

3 years 3 weeks ago #7956 by Krell
Replied by Krell on topic weather
random.c holds circle_random()
```#define	m  (unsigned long)2147483647
#define	q  (unsigned long)127773

#define	a (unsigned int)16807
#define	r (unsigned int)2836

/* F(z)	= (az)%m
**	= az-m(az/m)
**
** F(z)  = G(z)+mT(z)
** G(z)  = a(z%q)- r(z/q)
** T(z)  = (z/q) - (az/m)
**
** F(z)  = a(z%q)- rz/q+ m((z/q) - a(z/m))
** 	 = a(z%q)- rz/q+ m(z/q) - az */

static unsigned long seed;

void circle_srandom(unsigned long initial_seed)
{
seed = initial_seed;
}

unsigned long circle_random(void)
{
int lo, hi, test;

hi   = seed/q;
lo   = seed%q;

test = a*lo - r*hi;

if (test > 0)
seed = test;
else
seed = test+ m;

return (seed);
}```

So circle_random() could possibly return 2147483647 as a value. If we use 0 and 1 then you're finding 2147483647 % 2 = 1. If we use 1 and 2 then you're finding 2147483647 % 3 = 1.

Maybe there's a different randomizer in the code you could try?

• JTP
• Topic Author
• Offline
• Platinum Member
3 years 3 weeks ago #7957 by JTP
Replied by JTP on topic weather
Just tried 1, 10 and a <= 5

that seems to work, just saw the else message
Hope i next time see the if message

• JTP
• Topic Author
• Offline
• Platinum Member
3 years 3 weeks ago #7958 by JTP
Replied by JTP on topic weather
hmm same again

3 years 3 weeks ago #7959 by lacrc
Replied by lacrc on topic weather
When you say "same again" is it because they repeated or is it because you were expecting them to alternate?

With rand_number() you're not guaranteed to have one outcome after the other, they have a 50% chance of happening but that's it, they can repeat.

If you want to have your code alternating results then you need to set a variable (probably a global in your code?) and have the weather use it as a check as you see necessary.

• JTP
• Topic Author
• Offline
• Platinum Member
3 years 3 weeks ago #7960 by JTP
Replied by JTP on topic weather
yea saw the if message now :)

3 years 3 weeks ago - 3 years 3 weeks ago #7961 by Krell
Replied by Krell on topic weather
Right, increase the the range between your starting and end values then pick the median as the cutoff point. Nicely done.

Is the "if message" also represented by "..."? I'm a little lost.
Last edit: 3 years 3 weeks ago by Krell.

• JTP
• Topic Author
• Offline
• Platinum Member
3 years 3 weeks ago #7962 by JTP
Replied by JTP on topic weather
just tried to cut away as much unnessesary stuff as possible for easier reading hense then .... instead of a long line :)