7 Replies Latest reply on Jul 26, 2007 1:47 AM by cybersabah

# Random number

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?
• ###### 1. Re: Random number
Try this,
<cfdump var="#RandRange(1,4)##RandRange(1,4)##RandRange(1,4)##RandRange(1,4)#">

Thanks
• ###### 2. Re: Random number
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.
• ###### 3. Re: Random number
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)
• ###### 4. Re: Random number
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[j];
}
return s;
}
• ###### 5. Re: Random number
Which parts don't you understand?

--
• ###### 6. Re: Random number
To wirte the CF coding..
• ###### 7. Re: Random number
To write the CF coding..