-
[F8] non repeating random numbers
Hi FlashKit
I am trying to make a function that will keep successive random numbers from being the same. In other words, I am generating a bunch of random numbers within a fairly small range, and do not want the same 2 numbers in a row. I am trying to do this by making a function, generateInitialRandomNumber(), that generates a random number “apples” then calls compare() which generates another random number “oranges” and compares it to the first. If the numbers are the same, compare() keeps looping until it gets a different number. So far, so good. I can get compare() to find the different number I want. I just can’t seem to get that number out of the compare() function and back into to the generateInitialRandomNumber() function. If I use “return,” I keep getting the number that matches “apples,” not the different one. Is there somewhere I can put a “break” statement, or somewhere else I can put the “return” statement? Is there a better approach to this problem? Thanks.
Zaffer
Code:
generateInitialRandomNumber();
function generateInitialRandomNumber(){
var apples:Number = randRange(0, 3);
trace("apples: " + apples);
goalNumber = compare(apples);
//goalNumber should be different from "apples"
trace("goalNumber: " + goalNumber);
trace(" ");
}
function compare(apples){
var oranges:Number = randRange(0, 3);
//can generate nonmatching number of oranges here
trace("oranges: " + oranges);
if(oranges == apples){
compare(apples);
}
//can't figure out how to get nonmatching oranges out of function
return oranges;
}
function randRange(min:Number, max:Number):Number {
var randomNum:Number = Math.round(Math.random() * (max - min + 1) + (min - .5));
return randomNum;
}
-
I think that your method is good, and would work, but it is pretty processor intensive. For me, if I was generating a LOT of random, non-duplicating numbers, I would first generate an array from 0-100 (or whatever my number range is) and then use a new array to create the random order. for example
edit, I forgot to explain the processor part... using this way, we have 120 loops. But if you are checking each item if there is a match or not ends up with a possibility of thousands of loops.
Code:
var range:Number = 100;
var holder_array:Array = new Array();
for (var j:Number = 0; j < range; j++) {
holder_array.push(j);
}
var randomNumbers_array:Array = new Array();
var howMany:Number = 100;
for (var i:Number = 0; i<howMany; i++) {
var myRandNum:Number = randRange(0, holder_array.length-1);
randomNumbers_array.push(holder_array[myRandNum]);
holder_array.splice(myRandNum,1);
}
function randRange(min:Number, max:Number):Number {
var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
return randomNum;
}
Last edited by malihk; 05-08-2007 at 05:06 PM.
-
Hi Malihk,
Thanks for your idea. I tried it and it works well, as far as producing a non-repeating sequence within a given range. However, I am working with very small ranges, and don’t want them to repeat “end-to-end either.” If you want to see what I am doing, go to www.bitsong.com/necklace.html. I built a colorful necklace that follows the mouse, based on Keith Peters book, “ActionScript Animation, Making Things Move.” As you can see if you play with it, the colors repeat sometimes. I was looking for a function that I could call that would produce a single non-repeating number between 0 and 7. Yours would do that, but then it might repeat an “end-to-end” number like this: 4,0,2,6,1,5,3 2,0,3,4,1,6,5 5,3,2,4,6,0,1.
Besides, to tell you the truth, I don’t know how to use an array in this context. I need a function that generates single numbers because my knowledge of arrays and how to use them is limited at this point. Thanks for your interest and help.
Zaffer
-
Zaffer -
No worries man. I think I get what you are saying. About the array first though. Look in the AS docs about PUSH and SPLICE I promise that this will take your coding to a new level once you start working with array. As far as a small range, you can change some vars in that script I posted and you should be all right. Here are the major vars
range = 100 // this is just a var saying from 0-99 that you want to get numbers
// between those points, you could change it to 7
howMany = 100 // saying you want a return of 100 numbers out of your range
///(could be 10 or 12 or whatever
randomNumbers_array // this is what you get in the end, an array of random numbers
//defined by your variables above
hope that helps!
-
Thanks, I'll give it a try.
-
Maybe try something like this...
Code:
var howMany = 8;
beadCount.text = String(howMany);
//
initializeBeads = function () {
var RANGE = 8;
var sets = Math.ceil(howMany / RANGE);
var holder_array = new Array();
var randomNumbers_array = new Array();
//
for (var j = 0; j < RANGE; j++) {
holder_array.push(j);
}
for (var i = 0; i < sets + 1; i++) {
holder_array.sort(function (a, b) { return Math.round(Math.random() * 2) - 1;});
for (var j = 0; j < RANGE; j++) {
randomNumbers_array.push(holder_array[j]);
}
}
for (var n = RANGE; n < randomNumbers_array.length; n++) {
if (randomNumbers_array[n - 1] == randomNumbers_array[n]) {
randomNumbers_array.splice(n, 1);
}
}
//
for (var k=0; k<howMany; k++){
trace(randomNumbers_array[k])
}
//
};
//
btnBeadCount.onPress = function() {
howMany = Number(beadCount.text);
initializeBeads();
};
//
initializeBeads()
-
When in doubt ask
Sorry to intrude:
I doubt if this is what you needed.
Any how please see the two attached FLAs (F8).
Note:These FLAs were earlier published in these forums,
when and by whom I have no record.
Last edited by Eager Beaver; 08-19-2007 at 02:25 PM.
<signature removed by admin>
-
Can't get easier then this posted by: A MODIFIED DOG
mainArr = [];
for (var n=1;n!=16;n++){ mainArr.push(n); }
trace(mainArr);
tempArr = mainArr.concat(); // copy the main array
tempArr.sort(function(){return Math.floor(Math.random()*3)-1}); // randomise
tempArr.splice(10,5); // remove the last five elements
trace(tempArr);
-
got Dawsonk's to work
Hi FlashKit, Thanks for all your ideas. I looked at them and decided to try to work with Dawsonk's since he had adapted Malihk's which I already understood -- sort of. I got initializeBeads() to work with my applyColor() function by moving the code where the individual objects in the array are taken out again, one by one -- which I did not know how to do (thanks Dawsonk) -- to the applyColor() function and fed the resulting random numbers into my switch statement, and it worked! Thanks.
Code:
function initializeBeads(howMany):Array{
var RANGE = 8;
var sets = Math.ceil(howMany / RANGE);
var holder_array = new Array();
var randomNumbers_array = new Array();
//
for (var j = 0; j < RANGE; j++) {
holder_array.push(j);
}
for (var i = 0; i < sets + 1; i++) {
holder_array.sort(function (a, b) { return Math.round(Math.random() * 2) - 1;});
for (var j = 0; j < RANGE; j++) {
randomNumbers_array.push(holder_array[j]);
}
}
for (var n = RANGE; n < randomNumbers_array.length; n++) {
if (randomNumbers_array[n - 1] == randomNumbers_array[n]) {
randomNumbers_array.splice(n, 1);
}
}
/* //moved this to applyColor() function
for (var k=0; k<howMany; k++){
trace(randomNumbers_array[k])
}
*/
//returned whole array
return randomNumbers_array;
};
applyColor(20);
function applyColor(ballCount){
master_array = initializeBeads(ballCount);
for (var i:Number = 0; i < ballCount; i++){
//learned how to get a series of single
//values back out on an array -- thanks Dawsonk!
trace(master_array[i])
switch(master_array[i]){
case 0:
redRange();
break;
case 1:
greenRange();
break;
case 2:
blueRange();
break;
case 3:
cyanRange();
break;
case 4:
magentaRange();
break;
case 5:
yellowRange();
break;
case 6:
orangeRange();
break;
case 7:
violetRange();
break;
}
colorBall(this["ball" + i], red, green, blue);
}
}
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
|