Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
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, 1291👍, 0💬
Popular Posts:
How To Enter Numeric Values as HEX Numbers? - MySQL FAQs - Introduction to SQL Basics If you want to...
What Are Named Parameters? - Oracle DBA FAQ - Creating Your Own PL/SQL Procedures and Functions Name...
What are unadjusted function points and how is it calculated? Unadjusted function points = ILF + EIF...
How do you override a defined macro? You can use the #undef preprocessor directive to undefine (over...
What Is C Language? The C programming language is a standardized programming language developed in t...