|
-
Odisey
Randomizing a bank of numbers
Hello,
Can someone help me figure this math. I have a bank of numbers. I have:
Twelve 1's
Twelve 2's
Twelve 3's
Twelve 4's
Twelve 5's
Twelve 6's
Twelve 7's
Twelve 8's
I want to randomly generate 3 digit numbers with the bank until the bank is empty. One limitation is none of the three digit numbers can have the same numbr in it twice .... like 223. And another is the same three digit number cannot be repeated, such as 345, 345.
Thank you,
Odisey
Much to learn.... Much to learn....
-
PHP Code:
Array.prototoype.contains = function(element){
var count = this.length;
while(count){
if(this[--count]==element){
return true;
};
};
return false;
};
function getrandomindex(array){
return Math.round(Math.random()*(array.length-1));
};
function getrandomelement(array){
return array[Math.round(Math.random()*(array.length-1))];
};
var bank = [];
for(var i=1;i<9;i++){
for(var j=0;j<12;j++){
bank.push(i);
};
};
trace(bank);
var returns = [];
function gettrips(){
if(bank.length < 3){
return false;
};
var a = getrandomindex(bank);
var b = getrandomindex(bank);
while(bank[a]==bank[b]){
b = getrandomindex(bank);
}
var c = getrandomindex(bank);
while(bank[b]==bank[c] || bank[a]==bank[c]){
c = getrandomindex(bank);
};
var d = [bank[a],bank[b],bank[c]].join("");
if(returns.contains(d)){
return gettrips();
};
bank.splice(a,1);
bank.splice(b,1);
bank.splice(c,1);
returns.push(d);
return d;
};
this.onMouseDown = function(){
gettrips();
trace(returns);
trace(bank.length);
};
-
Here is a reusable function I tend to use for random sort with no repetition. This will sort any array in random order, with each item appearing once. What it does is swap a element picked at random with the last element of the array. The 'pickable' set will be reduced by 1, swap, etc.
arrRnd = randomSort(arr)
PHP Code:
/** randomSort, no repetition */ function randomSort(itms) { var oItem:Object = new Object() var lastPos:Number = itms.length
for(var i = 0; i<itms.length; i++){ lastPos = itms.length-i-1 rnd = Math.floor(Math.random() * (lastPos)) oItem = itms[lastPos] itms[lastPos] = itms[rnd] itms[rnd] = oItem; } return itms; }
-
Odisey
Almost
 Originally Posted by moagrius
PHP Code:
Array.prototoype.contains = function(element){
var count = this.length;
while(count){
if(this[--count]==element){
return true;
};
};
return false;
};
function getrandomindex(array){
return Math.round(Math.random()*(array.length-1));
};
function getrandomelement(array){
return array[Math.round(Math.random()*(array.length-1))];
};
var bank = [];
for(var i=1;i<9;i++){
for(var j=0;j<12;j++){
bank.push(i);
};
};
trace(bank);
var returns = [];
function gettrips(){
if(bank.length < 3){
return false;
};
var a = getrandomindex(bank);
var b = getrandomindex(bank);
while(bank[a]==bank[b]){
b = getrandomindex(bank);
}
var c = getrandomindex(bank);
while(bank[b]==bank[c] || bank[a]==bank[c]){
c = getrandomindex(bank);
};
var d = [bank[a],bank[b],bank[c]].join("");
if(returns.contains(d)){
return gettrips();
};
bank.splice(a,1);
bank.splice(b,1);
bank.splice(c,1);
returns.push(d);
return d;
};
this.onMouseDown = function(){
gettrips();
trace(returns);
trace(bank.length);
};
This is good... but ,,, here is the output:
1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,3, 3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,5,5, 5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7, 7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8
Much to learn.... Much to learn....
-
Odisey
 Originally Posted by widged
Here is a reusable function I tend to use for random sort with no repetition. This will sort any array in random order, with each item appearing once. What it does is swap a element picked at random with the last element of the array. The 'pickable' set will be reduced by 1, swap, etc.
arrRnd = randomSort(arr)
PHP Code:
/**
randomSort, no repetition
*/
function randomSort(itms) {
var oItem:Object = new Object()
var lastPos:Number = itms.length
for(var i = 0; i<itms.length; i++){
lastPos = itms.length-i-1
rnd = Math.floor(Math.random() * (lastPos))
oItem = itms[lastPos]
itms[lastPos] = itms[rnd]
itms[rnd] = oItem;
}
return itms;
}
Maybe I can use this somehow.... Thank you
Much to learn.... Much to learn....
-
odisey - that's just the output of the bank of numbers. run that, then mouse down several times and you'll see it returning what you asked for.
-
maybe this example will be easier to understand. just open a new FLA, paste in frame 1's actions panel, and test movie.
PHP Code:
Array.prototoype.contains = function(element){
var count = this.length;
while(count){
if(this[--count]==element){
return true;
};
};
return false;
};
function getrandomindex(array){
return Math.round(Math.random()*(array.length-1));
};
var bank = [];
var returns = [];
for(var i=1;i<9;i++){
for(var j=0;j<12;j++){
bank.push(i);
};
};
function gettrips(){
if(bank.length < 3){
return false;
};
var a = getrandomindex(bank);
var b = getrandomindex(bank);
while(bank[a]==bank[b]){
b = getrandomindex(bank);
}
var c = getrandomindex(bank);
while(bank[b]==bank[c] || bank[a]==bank[c]){
c = getrandomindex(bank);
};
var d = [bank[a],bank[b],bank[c]].join("");
if(returns.contains(d)){
return gettrips();
};
bank.splice(a,1);
bank.splice(b,1);
bank.splice(c,1);
return d;
};
trace(gettrips());
trace(gettrips());
trace(gettrips());
trace(gettrips());
trace(gettrips());
trace(gettrips());
trace(gettrips());
trace(gettrips());
trace(gettrips());
trace(gettrips());
Last edited by moagrius; 08-01-2008 at 07:26 AM.
-
 Originally Posted by Odisey
Maybe I can use this somehow....
If you are after creating a bank of 12 times the values 1-8, before the function, try something like this.
PHP Code:
// build an array with 12 times the values 1-8 var arrVal:Array = new Array() for (i = 1; i <= 8; i++) { for (j = 0; j < 12; j++) { arrVal.push( i) } }
// sort the array in random order, no repetition arrRnd = randomSort(arrVal) // use the function given in the previous post
// optional, a pretty output commaSep = arrayCombine(arrRnd, ',') trace(commaSep)
/** arrayCombine */ function arrayCombine(arr, sep) { var str:String = '' for (i = 0; i < arr.length; i++) { str += arr[i] + sep } str = str.substr(0, str.length - sep.length) // removes trailing separator // faster than an if in the loop return str }
Last edited by widged; 08-01-2008 at 07:19 AM.
-
Note however that it doesn't get you any close from solving your problem of avoiding '223'.
You can try and add conditions like the one below...
PHP Code:
// sort the array in random order, no repetition arrRnd = randomSort(arrVal) // use the function given in the previous post var arrTriplet:Array = new Array() for (var i = 0; i < arrRnd.length; i++) { digit = arrRnd[i] if(arrTriplet[0] != undefined && digit == arrTriplet[0]) { next } if(arrTriplet[1] != undefined && digit == arrTriplet[1]) { next } arrTriplet.push(digit) if(arrTriplet.length == 3) { break } } triplet = arrayCombine(arrTriplet, '') trace (triplet)
but the script above doesn't check that you don't pick the same number twice (345, 345). With a bank approach, you will come across one problem after another.
Well, the thing is that you really don't need to start from 12 times 1-8 to get all possible combinations of 3 digits made of numbers from 1-8 ;-). The bank is somewhat irrelevant. Especially if each number occurs with the same probability (12 count). You are better off using permutations.
PHP Code:
var arrTriplets:Array = new Array() for (i = 1; i <= 8; i++) { for (j = 1; j <= 8; j++) { for (k = 1; k <= 8; k++) { if(i != j && j != k && k != i ) { arrTriplets.push("" + i + "" + j + "" + k) } } } }
arrRnd = randomSort(arrTriplets) commaSep = arrayCombine(arrRnd, ',') trace(commaSep)
(see posts above for functions randomSort and arrayCombine).
Last edited by widged; 08-01-2008 at 07:36 AM.
-
widged: how is arrayCombine any different than join? just curious...
-
 Originally Posted by moagrius
widged: how is arrayCombine any different than join? just curious...
I have been coding with PHP for the last month (before you ask, join also exists in php but as an alias of implode)... I couldn't remember the magic word and I had no book at hand at the time I replied. I ran a rapid search on the web and wasn't successful, so I bundled a function of my own :-)
Yup, always use native functions when available.
Last edited by widged; 08-02-2008 at 12:19 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|