Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

its urgent

 
amitmehta11979
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amit mehta

currently i am working in jsp. i want to generate random numbers
suppose there is 1000 questions with me i want that that 1000 question
should be generated randomly and one question should not repeate for the
long time. and all the question should come one after one.

 
Rahul S
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Amit
U can maintain an array of nos u want to generate
Generate a random no and store that in the array
Next time, first check whether the no. is there in the array
if yes then generate another no
else add the no to the array
In this way u can get unique random nos
hope this helps
Rahul
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How unique and non-repetitive do these numbers need to be?
If you just want decent random numbers, use java.util.Random.nextInt(1000).
If you want a guarantee that no question will be revisited until you've seen all others, but don't care overly about randomness, randomly generate an offset which is a prime relative to the number of questions (a normal prime will do):
���// generate a prime under 1000
���offset = generateRandomPrime(1000);
���// randomly choose the initial question
���question = random(1000);
Then going to the next question is as simple as adding the offset:
���question = (question + offset) % 1000;
Of course this means that, once you have the initial question and offset, you will be on a fixed track through the questions. But given the number of different primes you can generate, this is probably just fine.
If you don't like this and want to plot a truly random course through the questions without repeating them, you will have to store the generated numbers as suggested.
- Peter

[This message has been edited by Peter den Haan (edited March 01, 2001).]
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"amitmehta11979",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please choose a new name which meets the requirements.
Your request may be urgent, but ignoring the "house rules" is not the best way to ask for help.
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic