Welcome to the Builder Academy

Question weather

More
18 Apr 2018 21:59 #7951 by JTP
weather was created by JTP
Trying to give the weather more variety:

Code:
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 ?

Please Log in or Create an account to join the conversation.

More
19 Apr 2018 13:39 - 19 Apr 2018 13:43 #7952 by krell
Replied by krell on topic weather
rand_number() in utils.c ends with the following line
Code:
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: 19 Apr 2018 13:43 by krell.

Please Log in or Create an account to join the conversation.

More
19 Apr 2018 17:19 #7953 by JTP
Replied by JTP on topic weather
Just tried 1, 2 and <2

Now i keep getting the first message instead.

Please Log in or Create an account to join the conversation.

More
19 Apr 2018 17:20 #7954 by JTP
Replied by JTP on topic weather
So might be a bracket problem, i never Can Seem to get em right.

Please Log in or Create an account to join the conversation.

More
19 Apr 2018 17:55 - 19 Apr 2018 18:05 #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: 19 Apr 2018 18:05 by krell.

Please Log in or Create an account to join the conversation.

More
19 Apr 2018 19:26 #7956 by krell
Replied by krell on topic weather
random.c holds circle_random()
Code:
#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?

Please Log in or Create an account to join the conversation.

Time to create page: 0.212 seconds