Opry99er Posted January 16, 2018 Share Posted January 16, 2018 I may perhaps have this, but I'm not sure... So I am doing some testing to see if I can avoid allocating a random number to a variable... rather using it as a pseudo-variable of sorts on its own. Here's my code: 100 IF RND>.5 THEN PRINT RND ELSE PRINT "LOW" 110 GOTO 100 And I get the following results I would expect it to print only numbers above .5 and print "LOW" for all numbers below .5. Theoretically, I should get about 50/50 numbers and "LOW"s. I'm not receiving those results I toiled with this for a little bit, but I think I understand why it is doing this. In the code, each iteration of "RND" is producing a different pseudo-random number between 0 and 1. So, that means that if I reference it twice--EVEN within the same IF THEN statement, it will give different numbers for each iteration. Therefore, (step by step): IF RND >.5 THEN.... (STOP) ** Let's give RND a value... like (.1653445043). So, at this point in the line, RND is actually less than .5, so the program will jump to the "ELSE" portion of the line and print "LOW". As it should. ***GOOD, WORKING WELL SO FAR!*** If, however, RND in this short snippit of my line is, say (.6542234185), then the computer will not jump to ELSE... in fact, it does what I am telling it to, and prints RND. Unfortunately (or fortunately... however you look at it) for my test, by the time the LINE says "PRINT RND," the RND that gets printed is in fact NOT the RND that I compared with .5 earlier in the line of code... it is a different number. That is because I have invoked RND a second time--and even if it occurs within the same line of code or even in the same statement within that line, it gives a different number. This is the only explanation I can come up with for this behavior. This is why we allocate a variable to capture a pseudo-random number... so we can store it and use it with predictable results.... and even if we use RND twice in the same IF THEN statement, it kicks out different results. This might be elementary, but it is pretty fascinating to me. Quote Link to comment Share on other sites More sharing options...
+mizapf Posted January 16, 2018 Share Posted January 16, 2018 This was the first thing that came to my mind when I saw your code: You can't expect to get the same value for RND when you query it again. 2 Quote Link to comment Share on other sites More sharing options...
+Lee Stewart Posted January 16, 2018 Share Posted January 16, 2018 Yeah—your code is doing exactly what you told it to do. What is interesting is that your results are exactly what you expected regarding “should get 50/50 numbers and ‘LOW’s"—just not the format you expected. You see, there are actually 30 responses in your 20 lines. There are 10 “LOW”s and 10 not-“LOW”s and, of the extra RND for each of the 10 not-“LOW”s, 5 qualify as LOW and 5 do not: The result is 15 of each—50/50. What may be surprising is that you get that result in such a small sample. ...lee 1 Quote Link to comment Share on other sites More sharing options...
Opry99er Posted January 16, 2018 Author Share Posted January 16, 2018 Yes, it is quite interesting all the way round Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.