Skip to main content
Known Participant
July 25, 2007
Question

Random number

  • July 25, 2007
  • 5 replies
  • 596 views
How do i write a CF script can do the random number searching for all the posibility number like...

Search a 4 digit number like: 1234

Result:
1234
1243
1324
1342
1423
1432
2134
2143
2314
2341
2413
2431
3124
3142
3214
3241
3412
3421
4123
4132
4213
4231
4312
4321

Anyone can help?
This topic has been closed for replies.

5 replies

Inspiring
July 26, 2007
Which parts don't you understand?

--
Adam
Known Participant
July 26, 2007
To wirte the CF coding..
Known Participant
July 26, 2007
Actually i looking for the permutation for the number i searching..

I found out the function of algorithm to generate permutations frpm Wikipedia, but no idea how to apply in CF script?

http://en.wikipedia.org/wiki/Permutation

Algorithm to generate permutations
For every number k () this following algorithm generates the corresponding permutation of the initial sequence :

function permutation(k, s) {
var int factorial:= 1;
for j= 2 to length(s) {
factorial:= factorial* (j-1); // factorial= (j-1)!
swap s[j- ((k / factorial) mod j)] with s;
}
return s;
}
Participant
July 25, 2007
If you're not too worried about pegging your server process, you could wrap it all in if statements. Write each value randomly generated to a table if it's not found in the table. Keep doing this until the table recordcount equals 24. (God that's messy, on second thought don't listen to me)
tclaremont
Inspiring
July 25, 2007
I can't be sure, but I think he is trying to arrive at all of the UNIQUE numbers that can be created with the digits 1,2,3 and 4.

If that is the case, the previous suggestion will not work. It will merely return a random number where each of the four digits could be either 1,2,3 or 4. It would also generate duplicates from time to time.
Participating Frequently
July 25, 2007
Try this,
<cfdump var="#RandRange(1,4)##RandRange(1,4)##RandRange(1,4)##RandRange(1,4)#">

Thanks