Post Reply 
(11C) Random Prime Number Generator
10-22-2018, 10:32 AM
Post: #21
RE: (11C) Random Prime Number Generator
Thanks Dieter for the review.

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.

So here is the Random Number section routine change:

LBL E
RAN#
EEX 4
x
INT
1001
X<>Y
X>Y (Test 7)
GTO 2
GTO E
------------------------
LBL 2
.
.
.
.
Find all posts by this user
Quote this message in a reply
10-22-2018, 11:03 AM
Post: #22
RE: (11C) Random Prime Number Generator
(10-21-2018 01:52 PM)John Keith Wrote:  
(10-21-2018 01:31 PM)Albert Chan Wrote:  (* Mathematica: primes below 10000 (1229 in total), all equally likely *)

Prime[Random[Integer, {1, 1229}]]

Or on the HP Prime (pun intended):

ithprime(rand(1229)+1)

Albert and John: the target is generating primes between 1000 and 9999. So the lowest index is 169 (for 1009) and the highest is 1229 (for 9973). I don't use Mathematica or the Prime, but I think you will know the correct syntax for a random index between 169 and 1229 (inclusively).

Dieter
Find all posts by this user
Quote this message in a reply
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
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
Find all posts by this user
Quote this message in a reply
10-22-2018, 12:28 PM
Post: #24
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.

It's okay but you could make it a bit shorter:
Code:
LBL E
1001
RAN#
EEX 4
x
INT
X≤Y
GTO E
------------------------
.
.
.
.

Quote:I'm using the conditional test that not allow it to get below 1000.
You might rather use 999 instead of 1001 if that's your intend.

Kind regards
Thomas
Find all posts by this user
Quote this message in a reply
10-22-2018, 12:31 PM
Post: #25
RE: (11C) Random Prime Number Generator
(10-22-2018 11:23 AM)Dieter Wrote:  LBL E
RAN#
EEX 4
x
INT

This generates a random number between 0 and 9999,999999.

You may want to remove that INT command in the last line.

Cheers
Thomas
Find all posts by this user
Quote this message in a reply
10-22-2018, 06:43 PM
Post: #26
RE: (11C) Random Prime Number Generator
(10-22-2018 12:31 PM)Thomas Klemm Wrote:  You may want to remove that INT command in the last line.

Sure. I will correct this now.

Dieter
Find all posts by this user
Quote this message in a reply
Post Reply 




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