Search  

QUESTION ABOUT RANDOM IMAGE DISPLAY

HomeForumsFlash Discussionquestion about random image display
45374 rahulbist
253 posts
Sold between 1 and 100 dollars Bought between 10 and 49 items

hi everyone,

I am making a flash application which displays images randomly.
I am using Math.random() function. The problem is that using the function, many times a particular images is displayed about 2 to 3 times at a stretch.

Do anyone have code for a strong random function to eliminate this problem?

please help me.

thanks in advance.

Posted 2 months ago
58251 djankey
304 posts
Item was featured Author was featured Referred at least one person Sold between 10 000 and 50 000 dollars Bought between 50 and 99 items

Randomize array:

var newArray:Array=new Array;

for(var i=0;i<10;i++) {                        
    generateRandomNumber(newArray,9);
}
trace(newArray);

function generateRandomNumber(myArray:Array, limit:Number):Void {
    var newNumber:Number = Math.round(Math.random()*limit);
    do {
        var addNumber:Boolean = true;
        for (item in myArray) {
            if (newNumber == myArray[item]) {
                addNumber = false;
                newNumber = Math.round(Math.random()*limit);
            }
        }
    } while (addNumber == false);
    myArray.push(newNumber);
}

Output example
8,5,9,1,4,3,7,6,2,0

Posted 2 months ago
54138 Pdesignx
445 posts
Exclusive author Author was featured Referred at least one person Sold between 1 000 and 5 000 dollars Bought between 1 and 9 items
Randomize array:
var newArray:Array=new Array;
for(var i=0;i<10;i++) {
generateRandomNumber(newArray,9); }
trace(newArray);
function generateRandomNumber(myArray:Array, limit:Number):Void {
var newNumber:Number = Math.round(Math.random()*limit);
do {
var addNumber:Boolean = true;
for (item in myArray) {<br> if (newNumber == myArray[item]) {
 addNumber = false;
 newNumber = Math.round(Math.random()*limit);
 }
 }
 }
 while (addNumber == false);
 myArray.push(newNumber);
 }

Output example 8,5,9,1,4,3,7,6,2,0

i think this is not generating random numbers… it’s just sorting numbers randomly.. and i think it does not do the job well.. because what if i want 16 random numbers with maximum number 9… your code will generate an infinite loop i think…

this is a fair way and every integer from 0 to maximum has same chance to be selected…

Math.floor(Math.random()*maximum+1)

if you have same numbers, that’s what we call chance :))

cheers..

Posted 2 months ago
38750 Paul_Ferrie
34 posts
Sold between 100 and 1 000 dollars Bought between 1 and 9 items

why not create an array of your image names then randomize the array removing each element as it is displayed. then once your array is empty recreate it. that way it you can be sure it is random and there will be know duplicate images loaded.

just a thought

Posted 2 months ago
38635 flasher3015
382 posts
Exclusive author Author was featured Referred at least one person Sold between 5 000 and 10 000 dollars Bought between 10 and 49 items
if you have same numbers, that’s what we call chance :))
I can see that you have no idea of what you’re talking :D If you generate a number from 1 to 10 randomly, EVERY TIME you will get at least 3 duplicates :D

If you generate 10 times numbers from 1 to 10, here is what Flash gets you:
1: 1,5,1,6,3,4,10,5,1,5
2: 3,6,2,8,6,2,6,2,7,6
3: 6,7,0,5,1,6,8,1,8,7
4: 1,3,3,2,8,5,6,1,2,6
5: 4,7,5,9,0,2,9,4,1,5
6: 1,5,6,8,7,7,4,8,7,0
7: 2,8,4,4,6,9,7,1,1,7
8: 3,8,1,4,3,9,0,6,6,10
9: 10,4,7,7,4,6,8,5,4,6
10: 6,0,1,5,3,10,8,0,1,9

This is not OK :) There is NO SOLUTION with all numbers from 1 to 10 :) So in conclusion: Djankey’s solution is used by almost all developers, because it’s the simplest and best way to do it :D

Posted 2 months ago
58251 djankey
304 posts
Item was featured Author was featured Referred at least one person Sold between 10 000 and 50 000 dollars Bought between 50 and 99 items
Djankey’s solution is used by almost all developers, because it’s the simplest and best way to do it :D

That’s right. Thank you flasher3015 ;)


Posted 2 months ago
38635 flasher3015
382 posts
Exclusive author Author was featured Referred at least one person Sold between 5 000 and 10 000 dollars Bought between 10 and 49 items
Djankey’s solution is used by almost all developers, because it’s the simplest and best way to do it :D

That’s right. Thank you flasher3015 ;)


Anytime ;)

Posted 2 months ago
54138 Pdesignx
445 posts
Exclusive author Author was featured Referred at least one person Sold between 1 000 and 5 000 dollars Bought between 1 and 9 items

i can see, you don’t get what you read… i said, if you pick 10 numbers from 0 to 9 and every one of them are different, it’s not called random.. it’s just a kind of array suffleing (randomly sorting)...
and as i say before did you tried to get 20 numbers from 0 to 9 with his code?? that’s not very usefull i think… unless an infinite loop makes you happy…
good luck with your awesome(!) codes..

Posted 2 months ago
38635 flasher3015
382 posts
Exclusive author Author was featured Referred at least one person Sold between 5 000 and 10 000 dollars Bought between 10 and 49 items

the guy wanted a method to show a set of photos in a random way(randomized array of X images). I don’t know what you want more than this :D The best way to do that is djankey’s solution.

Posted 2 months ago
28144 SaafiDesign
1420 posts
Exclusive author Item was featured Author was featured Referred at least one person Reviewer Sold between 10 000 and 50 000 dollars Bought between 1 and 9 items
the guy wanted a method to show a set of photos in a random way(randomized array of X images). I don’t know what you want more than this :D The best way to do that is djankey’s solution.

djankey’s solution is doing the job but is not efficient because for 10 random numbers it might iterate the loop 1000 times. The simple way is to fill an array of 10 (or whatever is the required) numbers and then just shuffle them.

Posted 2 months ago