(11C) Random Prime Number Generator
|
10-22-2018, 11:23 AM
(This post was last modified: 10-22-2018 06:44 PM by Dieter.)
Post: #23
|
|||
|
|||
RE: (11C) Random Prime Number Generator
(10-22-2018 10:32 AM)Gamo Wrote: I have updated the program but I try different approach and not sure if this version is OK or not. I tested many times and the random prime number is The method you chose now does this: LBL E RAN# EEX 4 x This generates a random number between 0 and 9999,999999. INT This makes it a random integer between 0 and 9999. 1001 X<>Y X>Y (Test 7) GTO 2 This accepts the numbers and continues if the number is greater than 1001, i.e. at least 1002. GTO E Otherwise a new number is generated. So you first calculate a number between 0 and 9999 and then you check if it is OK. But why don't you simply calculate a suitable number directly, as suggested? The general rule for a random integer between a and b (inclusively) is: random integer = INT((b–a+1)*RAN#) + a So for a number between a=1000 and b=9999 the calculation is random integer = INT((9000)*RAN#) + 1000 Let's try again: LBL E RAN# 9000 x INT This generates a random integer between 0 and 8999: 0, 1, 2, 3, ... , 8997, 8998, 8999. 1000 + This makes it a number between 1000 and 9999: 1000, 1001, 1002, 1003, ... 9997, 9998, 9999. Simple as that. ;-) You then turn this into an odd number (2 ÷ INT 2 x 1 +). The method I suggested directly calculates an odd number: First a random integer between 0 and 4499 is generated (0, 1, 2, 3, ... , 4497, 4498, 4499) Then this is doubled which yiels 0, 2, 4, 6, ... , 8994, 8996, 8998. Finally 1001 is added. This gives 1001, 1003, 1005, 1007, ... , 9995, 9997, 9999. Dieter |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)