|
Post by bitvast on Dec 31, 2014 16:57:18 GMT 1
Happy new year fellow BaConites. This little program finds the probability of at least one repeat in a sequence of 4 random numbers (taken from 4). CONST N = 100000 CONST dim = 4 CONST outcomes = 4 DECLARE nums[dim] cnt# = 0
FOR i = 1 TO N FOR j = 0 TO dim-1 nums[j] = RANDOM(outcomes) + 1 NEXT j 'are numbers unique? '----------------- SORT nums FOR k = 1 TO dim-1 IF nums[k] = nums[k-1] THEN INCR cnt# BREAK END IF NEXT k '----------------- NEXT i
PRINT cnt#/N 0.90559 The following program is the same but hands over finding a repeat to a SUB. However, it returns a different result. CONST N = 100000 CONST dim = 4 CONST outcomes = 4 DECLARE nums[dim] cnt# = 0
SUB find_duplicates 'are numbers unique? '----------------- SORT nums FOR k = 1 TO dim-1 IF nums[k] = nums[k-1] THEN INCR cnt# BREAK END IF NEXT k '----------------- END SUB
FOR i = 1 TO N FOR j = 0 TO dim-1 nums[j] = RANDOM(outcomes) + 1 NEXT j find_duplicates NEXT i
PRINT cnt#/N 0.57753 All variables are global. Have I overlooked something? Thanks in advance for any help!
|
|
|
Post by Pjot on Dec 31, 2014 21:54:58 GMT 1
Hi bitvast, Well, what can I say... you did it again The problem is with SORT. It needs to retrieve the dimension of the array. When SORT is used in the main program, the dimension is detected correctly. When SORT is used in a SUB or FUNCTION for a locally declared array, the dimension is detected correctly also. But in your example, SORT is used within a SUB and it needs to find the dimension of a globally declared array. This did not work, and is still a bug caused by the 3.x code overhaul in the beginning of this year. The fix is available in the latest beta. Thanks, Peter
|
|
|
Post by bitvast on Jan 1, 2015 10:40:44 GMT 1
Hi Peter,
Excellent. Thanks for the fix, all good now.
|
|