(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
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
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
not get below 1001 yet. I'm using the conditional test that not allow it to get
below 1000.

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 »

 Messages In This Thread (11C) Random Prime Number Generator - Gamo - 10-20-2018, 01:08 PM RE: (11C) Random Prime Number Generator - Dieter - 10-20-2018, 07:08 PM RE: (11C) Random Prime Number Generator - Gamo - 10-21-2018, 03:22 AM RE: (11C) Random Prime Number Generator - Dieter - 10-21-2018, 05:09 PM RE: (11C) Random Prime Number Generator - Thomas Klemm - 10-21-2018, 08:45 AM RE: (11C) Random Prime Number Generator - Dieter - 10-21-2018, 04:41 PM RE: (11C) Random Prime Number Generator - Albert Chan - 10-21-2018, 01:31 PM RE: (11C) Random Prime Number Generator - John Keith - 10-21-2018, 01:52 PM RE: (11C) Random Prime Number Generator - Dieter - 10-22-2018, 11:03 AM RE: (11C) Random Prime Number Generator - Dieter - 10-21-2018, 05:20 PM RE: (11C) Random Prime Number Generator - Albert Chan - 10-21-2018, 07:02 PM RE: (11C) Random Prime Number Generator - Albert Chan - 10-22-2018, 01:02 AM RE: (11C) Random Prime Number Generator - Gene - 10-21-2018, 07:47 PM RE: (11C) Random Prime Number Generator - Dieter - 10-21-2018, 09:09 PM RE: (11C) Random Prime Number Generator - Thomas Klemm - 10-21-2018, 08:47 PM RE: (11C) Random Prime Number Generator - Thomas Klemm - 10-21-2018, 09:03 PM RE: (11C) Random Prime Number Generator - Gene - 10-21-2018, 10:02 PM RE: (11C) Random Prime Number Generator - Thomas Klemm - 10-22-2018, 03:46 AM RE: (11C) Random Prime Number Generator - Thomas Klemm - 10-22-2018, 04:10 AM RE: (11C) Random Prime Number Generator - Thomas Klemm - 10-22-2018, 09:11 AM RE: (11C) Random Prime Number Generator - Thomas Klemm - 10-22-2018, 09:22 AM RE: (11C) Random Prime Number Generator - Gamo - 10-22-2018, 10:32 AM RE: (11C) Random Prime Number Generator - Dieter - 10-22-2018 11:23 AM RE: (11C) Random Prime Number Generator - Thomas Klemm - 10-22-2018, 12:28 PM RE: (11C) Random Prime Number Generator - Thomas Klemm - 10-22-2018, 12:31 PM RE: (11C) Random Prime Number Generator - Dieter - 10-22-2018, 06:43 PM

User(s) browsing this thread: 1 Guest(s)