IGNORED

# Interesting RND behavior

## Recommended Posts

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.

##### Share on other sites

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.

##### Share on other sites

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

##### Share on other sites

Yes, it is quite interesting all the way round

## 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.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.