Categories:
.NET (961)
C (387)
C++ (185)
CSS (84)
DBA (8)
General (31)
HTML (48)
Java (641)
JavaScript (220)
JSP (109)
JUnit (31)
MySQL (297)
Networking (10)
Oracle (562)
Perl (48)
Perl (9)
PHP (259)
PL/SQL (140)
RSS (51)
Software QA (28)
SQL Server (5)
Struts (20)
Unix (2)
Windows (3)
XHTML (199)
XML (59)
Other Resources:
How can I return a sequence of random numbers which dont repeat at all?
How can I return a sequence of random numbers which dont repeat at all?
✍: Guest
What you're looking for is often called a ``random permutation'' or ``shuffle.'' One way is to initialize an array with the values to be shuffled, then randomly interchange each of the cells with another one later in the array:
int a[10], i, nvalues = 10;
for(i = 0; i < nvalues; i++)
a[i] = i + 1;
for(i = 0; i < nvalues-1; i++) {
int c = randrange(nvalues-i);
int t = a[i]; a[i] = a[i+c]; a[i+c] = t; /* swap */
}
where randrange(N) is rand() / (RAND_MAX/(N) + 1)
2015-07-24, 961👍, 0💬
Popular Posts:
What is effort variance? Effort Variance = (Actual effort – Estimated Effort) / Estimated Effort.
How Do I Run JUnit Tests from Command Window? To run JUnit tests from a command window, you need to ...
Can an anonymous class be declared as implementing an interface and extending a class? An anonymous ...
What Is C Language? The C programming language is a standardized programming language developed in t...
How did you implement UML in your project ? PART II Implementation phase / Coding phase (Class diagr...