# Sequence of numbers, ordered randomly

• a.k.a. dM
• Posts: 5146
• Loc: SC-USA

3+ Months Ago

I need to create a string of, up to, 4 numbers. The values of the numbers have to include the numbers 1 through x, be seperated by commas, and can't repeat any digits.
(whoa. confusing myself)

For instance:

1,2,3,4
4,3,2,1
3,2,4,1
2,1,4,3
4,2,1,3
etc.

I also need this function to accept the number of digits as an argument, so it could produce any of these strings:

2,1,3
1,2,3
3,2,1

I know I've done this before, but, for the life of me, I can't remember how or where.

Hints? Solutions?
• a.k.a. dM
• Posts: 5146
• Loc: SC-USA

3+ Months Ago

I got it. I realized it while editing my post for the 3rd or 4th time.

Code: [ Select ]
function generateString(seed) {
for (i=1; i<=seed; i++) {
if (rString.length>1) {
ranVal = Math.ceil(Math.random()*2);
switch (ranVal) {
case 1 :
rString = String(rString)+String(i+",");
break;
case 2 :
rString = String(i+",")+String(rString);
break;
}
} else {
rString = i+",";
}
}
rString = rString.substring(0, rString.length-1);
}
generateString(4);
trace(rString);
1. function generateString(seed) {
2.     for (i=1; i<=seed; i++) {
3.         if (rString.length>1) {
4.             ranVal = Math.ceil(Math.random()*2);
5.             switch (ranVal) {
6.             case 1 :
7.                 rString = String(rString)+String(i+",");
8.                 break;
9.             case 2 :
10.                 rString = String(i+",")+String(rString);
11.                 break;
12.             }
13.         } else {
14.             rString = i+",";
15.         }
16.     }
17.     rString = rString.substring(0, rString.length-1);
18. }
19. generateString(4);
20. trace(rString);

If you see a way to streamline this, or have a better solution...
• Fart Bubbles
• Posts: 13506
• Loc: Florida

3+ Months Ago

Still using seed, has an optional delimiter param
Code: [ Select ]
function ranString(seed, oDel) {
oDel==undefined ? oDel="" : null;
var seq = [];
for (i=1; i<=seed; i++) {
Math.round(Math.random())>0 ? seq.push(i) : seq.unshift(i);
}
return seq.join(oDel);
}

trace(ranString(4));
trace(ranString(4,","));
1. function ranString(seed, oDel) {
2.     oDel==undefined ? oDel="" : null;
3.     var seq = [];
4.     for (i=1; i<=seed; i++) {
5.         Math.round(Math.random())>0 ? seq.push(i) : seq.unshift(i);
6.     }
7.     return seq.join(oDel);
8. }
9. trace(ranString(4));
10. trace(ranString(4,","));
• a.k.a. dM
• Posts: 5146
• Loc: SC-USA

3+ Months Ago

Very nice joebert! Thank you.

I've never used this syntax before:

Code: [ Select ]
oDel==undefined ? oDel="" : null;

I like it. Thank you Joebie-Wan!
• Fart Bubbles
• Posts: 13506
• Loc: Florida

3+ Months Ago

• a.k.a. dM
• Posts: 5146
• Loc: SC-USA

3+ Months Ago

Fabulous!!! Further thanks to LIB.

## Post Information

• Total Posts in this topic: 6 posts
• Users browsing this forum: No registered users and 8 guests
• You cannot post new topics in this forum
• You cannot reply to topics in this forum
• You cannot edit your posts in this forum
• You cannot delete your posts in this forum
• You cannot post attachments in this forum