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, 1165👍, 0💬
Popular Posts:
What is triple constraint triangle in project management ? Project Management triangle is depicted a...
How To Export Data to an XML File? - Oracle DBA FAQ - Introduction to Oracle SQL Developer If you wa...
How do I install JUnit? First I will download the lastest version of JUnit. Then I will extract all ...
interview.FYIcenter.com offers a collections of interview questions and answers for software and Web...
What will be printed as the result of the operation below: main() { int x=10, y=15; x = x++; y = ++y...