How to display a random sequence numbers in the range
1 - 1000 , every number should be displayed once and no repetition~~~
thx~~~
How to display a random sequence numbers in the range
1 - 1000 , every number should be displayed once and no repetition~~~
thx~~~
For one idea, use an array saying which ones you've used already.
The simplest way to go about this (a random range with no duplicates) when the range is relatively small is to randomly shuffle an array initialized to [1..N). A naive implementation might look like this:
Other solutions using an array are very inefficient, requiring you to save and search the array to see if a number has already been displayed. If the range is very large then an array really isn't the right data structure.Code:for (i = 0; i < n - 1; i++) swap(&list[i], &list[i + ( rand() % (n - i) )]);
My best code is written with the delete key.
Unefficient, but fun, and easily understandable code.Originally Posted by Prelude
Code:#define lOOO 1000 #define l000 0 #define l00O 1 int lOO0[lOOO]={l000}; int lO00=l000; int l0O0=l000; while( lO00 < lOOO-l00O ) { if(lOO0[l0O0=rand()%lOOO]++==l000) { printf("%5d", l0O0 ); lO00++; } }
Quzah.
Last edited by quzah; 12-04-2004 at 03:32 PM. Reason: Made it more readable. ;)
Hope is the first step on the road to disappointment.
You just love defines dont you quzah??
Yes. Yes I do.
Quzah.
Hope is the first step on the road to disappointment.
thx to everyone~~~
interesting~~i like itOriginally Posted by quzah