473,468 Members | 1,329 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

random prime number generation for RSA

4 New Member
How do I determine random prime numbers in RSA? I have already generated numbers but can't check if prime or not.

Here is the part of generation
Expand|Select|Wrap|Line Numbers
  1.     public static void main(String[] args) throws IOException {
  2. int q=0;
  3. int p=0;
  4.  
  5. int maxValue = 100;
  6. int maxNumber = 2;
  7.  
  8. Random x = new Random ();
  9.  
  10. for (int k=1; k < maxNumber; k ++) {
  11. p = (Math.abs(x.nextInt () % maxValue ));
  12.  
  13. System.out.println("p: "+p);
May 11 '10 #1

✓ answered by jkmyoung

You probably want a do-while loop
Expand|Select|Wrap|Line Numbers
  1. do{
  2. Set your random number
  3. } while (random number is not prime);
Then you can only leave the loop if your number IS prime.


Also, consider changing your for loop condition from
2*i<n TO
i*i <= n

Right now, 4 wouldn't work, because when i = 2, 2 * i = 2 * 2 = 4 which is not < n.

4 11963
jkmyoung
2,057 Recognized Expert Top Contributor
You're looking for a Primality test. Look at some of the algorithms given, and decide which one you want to implement. Come back with your problems, if you have any.
May 12 '10 #2
hebaahmed
4 New Member
Expand|Select|Wrap|Line Numbers
  1. public static boolean isPrime(int n) {
  2.     for(int i=2;2*i<n;i++) {
  3.         if(n%i==0)
  4.             return false;
  5.     }
  6.     return true;
  7. }
  8.  
  9. public static void main(String[] args) throws IOException {
  10. int q=0;
  11. int p=0;
  12.  
  13. int maxValue = 100;
  14. int maxNumber = 2;
  15.  
  16. Random x = new Random ();
  17.  
  18. for (int k=1; k < maxNumber; k ++) {
  19. p = (Math.abs(x.nextInt () % maxValue ));
  20.  
  21. System.out.println("p: "+p); 
I already got prime test algorithm but I want to make that if it's prime number ok show p=number that has been generated and being prime else break and generate another one and check again I try alot but i don't know the problem.
May 12 '10 #3
jkmyoung
2,057 Recognized Expert Top Contributor
You probably want a do-while loop
Expand|Select|Wrap|Line Numbers
  1. do{
  2. Set your random number
  3. } while (random number is not prime);
Then you can only leave the loop if your number IS prime.


Also, consider changing your for loop condition from
2*i<n TO
i*i <= n

Right now, 4 wouldn't work, because when i = 2, 2 * i = 2 * 2 = 4 which is not < n.
May 12 '10 #4
hebaahmed
4 New Member
Thanks for your reply. I can do it.
May 12 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

36
by: Dag | last post by:
Is there a python module that includes functions for working with prime numbers? I mainly need A function that returns the Nth prime number and that returns how many prime numbers are less than N,...
11
by: don | last post by:
Ok, this is a homework assignment, but can you help me out anyway...... I need a routine for figuring out if a number inputted by the user is a prime number or not...... all I'm asking for is Not...
10
by: Ioannis Vranos | last post by:
I want to create some random numbers for encryption purposes, and i wonder if the following scheme makes it more hard to guess the underneath number generation pattern, than the plain use of...
4
by: Dimos | last post by:
Hello All, I need some help with random number generation. What I need exactly is: To create a few thousand numbers, decimal and integers, between 5 and 90, and then to export them as a...
20
by: Tuvas | last post by:
I have made and recently posted a libary I made to do Modular Arithmetic and Prime numbers on my website at http://www.geocities.com/brp13/Python/index.html . I am currently in a crypotology...
4
by: SweetLeftFoot | last post by:
Hello, i have designed some code that works out the first 250 prime numbers and prints them to the screen. However i need to implement 2 functions, one of which returns a 1 if the number is a prime...
21
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
2
by: sudankanakavel | last post by:
i need a random prime number generator which will generate prime numbers for given range for eg 22222 to 99999 operating system : windows language : java
6
by: Fan924 | last post by:
How do I add random number generation to this background image slide show? Everything I try kills if. TIA _____________________________________ <html> <head> <meta http-equiv="Content-Type"...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.