• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Prime numbers selected at random

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends, can you please tell me that how can i get the prime numbers selected at random?
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) build a list of prime numbers
2) select one at random.

In all seriousness, you need better specs. Since there are an infinite number of prime numbers, you can't just pick one at random any more than you can pick a rational number at random. You need to (at the very least) define the parameters...is there a range of prime numbers you want to select from (i.e. prime numbers less than 100?) or only the first 50 prime numbers?

Then, you start writing out your algorithm - by hand, and in English (or whatever natural language you are most comfortable with). I have already done the first iteration for you. Start refining each step - how will you build a list of prime numbers (and that will depend on the parameters you define above)?

The nice thing about programming and designing this way is that steps 1 and 2 above are COMPLETELY INDEPENDENT of one another. Selecting an item from a list is not impacted at ALL by how I build a list or what is in it. And building a list of prime numbers is unaffected by what I do with the list when it is done. That lets you focus in on the one part of the problem at a time, and not worry about anything else.

When you have part 1 done, tested, validated, re-tested, and re-tested again, you can just plug it in for when you work on part 2.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear in mind that you're going to need a better defined requirement than that. There are an infinite number of prime numbers, so it's theoretically impossible to pick a random one with an even probability distribution.

And welcome to The Ranch!
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

adi bashir wrote:hi friends, can you please tell me that how can i get the prime numbers selected at random?


What the others have said is absolutely right, but you might want to check out the BigInteger class.

Winston
 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:1) build a list of prime numbers
2) select one at random.

In all seriousness, you need better specs. Since there are an infinite number of prime numbers, you can't just pick one at random any more than you can pick a rational number at random. You need to (at the very least) define the parameters...is there a range of prime numbers you want to select from (i.e. prime numbers less than 100?) or only the first 50 prime numbers?

Then, you start writing out your algorithm - by hand, and in English (or whatever natural language you are most comfortable with). I have already done the first iteration for you. Start refining each step - how will you build a list of prime numbers (and that will depend on the parameters you define above)?

The nice thing about programming and designing this way is that steps 1 and 2 above are COMPLETELY INDEPENDENT of one another. Selecting an item from a list is not impacted at ALL by how I build a list or what is in it. And building a list of prime numbers is unaffected by what I do with the list when it is done. That lets you focus in on the one part of the problem at a time, and not worry about anything else.

When you have part 1 done, tested, validated, re-tested, and re-tested again, you can just plug it in for when you work on part 2.



or you could investigate TDD (test driven developement), but still the 2 sections are good.
here is a good example once you have read wikipedia to find out what it is http://blog.coryfoy.com/2006/08/tdd-bowling-game-part-1/
reply
    Bookmark Topic Watch Topic
  • New Topic