68 posts
  • Has been part of the Envato Community for over 7 years
  • Has sold $1,000+ on Envato Market
  • Has collected 100+ items on Envato Market
  • Sells items exclusively on Envato Market
+2 more
botervet says

Isn’t there a way to Math.random()? For example with an array?

If yes… how can i do this?

Thanx

3742 posts Community Moderator
  • Helps us moderate the forums
  • Contributed a tutorial on Tuts+
  • Located in Netherlands
  • Made it to the Authors' Hall of Fame
+9 more
Joost Moderator says

You will have to make your question a bit more specific. There is a way to Math.random()... simply by using Math.random().. I don’t understand your question at all ;)

1161 posts
  • Has referred 10+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Had an item featured in an Envato Bundle
+4 more
FlashTang says
do you mean random index ?
yourArray[uint(Math.random()*yourArray.length)]
68 posts
  • Has been part of the Envato Community for over 7 years
  • Has sold $1,000+ on Envato Market
  • Has collected 100+ items on Envato Market
  • Sells items exclusively on Envato Market
+2 more
botervet says

Well,

In my previous post i asked if there was a an alternative for Math.random() becouse i can not use this in a google banner. you for example came up with this solution: var my_date:Date = new Date(); trace(my_date.valueOf())

But that doesn’t do the trick.

tried some other things…. also don’t work.

now some one told me i can fix the Math.random() issue by making an array that gives back 0 or 1

i just don’t know how to do this…. So i thing the thread title is wrong… It should have been how to create a random number with the use of an array :)

1677 posts Envato Elite Author
  • Has referred 10+ members
  • Has sold $125,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+6 more
Tean says

I dont understand why you cant use Math.random?

As a alternative you could pre populate array with random values:

var a:Array = [2,4,0,1,5];

and then pull out indexes in order.

for(var i:int=0; i < a.length; i++){
   trace(i);
}
68 posts
  • Has been part of the Envato Community for over 7 years
  • Has sold $1,000+ on Envato Market
  • Has collected 100+ items on Envato Market
  • Sells items exclusively on Envato Market
+2 more
botervet says
I dont understand why you cant use Math.random?

As a alternative you could pre populate array with random values:

var a:Array = [2,4,0,1,5];

and then pull out indexes in order.

for(var i:int=0; i < a.length; i++){
   trace(i);
}

Hi,

Thanx for the reply! the reason why i cant use Math.random() is becouse google ads doesn’t allow flashbanner to have a random value. If it has the file can not be uploaded!

Ill give your solution a try! :)

675 posts
  • Has referred 10+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
+6 more
dexterous says
I dont understand why you cant use Math.random?

As a alternative you could pre populate array with random values:

var a:Array = [2,4,0,1,5];

and then pull out indexes in order.

for(var i:int=0; i < a.length; i++){
   trace(i);
}

Hi,

Thanx for the reply! the reason why i cant use Math.random() is becouse google ads doesn’t allow flashbanner to have a random value. If it has the file can not be uploaded!

Ill give your solution a try! :)

I Don’t understand why are you not able to use the solution which was provided in http://activeden.net/forums/thread/google-banner/22211?page=1#207981

Math.random() returns number between 0 and 1

And the following:

var my_date:Date = new Date(); var myMathRand = my_date.getMilliseconds()/1000;

will return a number between 0 and .999

You have to understand that in your code, you are using Math.random() everytime you want a new random number, so you should create a new ‘myMathRand’ before your line of code where you want to use it.

68 posts
  • Has been part of the Envato Community for over 7 years
  • Has sold $1,000+ on Envato Market
  • Has collected 100+ items on Envato Market
  • Sells items exclusively on Envato Market
+2 more
botervet says

Google just doesn’t allow Math.random() in the code. It’s some kind of security thing.

For me

var my_date:Date = new Date(); var myMathRand = my_date.getMilliseconds()/1000;

Doesn’t work, becouse i’m also using Math.round cuz i need a round value of 1 or 0 for my animation. If I use milliseconds for this… it will not be random enough… then i will get a couple of 0 following each other and a couple of 1 following up on each other. It’s just not random enough!

var a:Array = [0,1]

for(var i:int=0; i < a.length; i++){ trace(i); }

Should work… but also this is not random enough… i need the output of the array to be more random like : 1,1,0,1,0,0,1,0,0,0 and not 0,1,0,1,0,1,0,1

How can i do this without using Math.random of random()?

Thanx :)

675 posts
  • Has referred 10+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
+6 more
dexterous says
Google just doesn’t allow Math.random() in the code. It’s some kind of security thing.

For me

var my_date:Date = new Date(); var myMathRand = my_date.getMilliseconds()/1000;

Doesn’t work, becouse i’m also using Math.round cuz i need a round value of 1 or 0 for my animation. If I use milliseconds for this… it will not be random enough… then i will get a couple of 0 following each other and a couple of 1 following up on each other. It’s just not random enough!

var a:Array = [0,1]

for(var i:int=0; i < a.length; i++){ trace(i); }

Should work… but also this is not random enough… i need the output of the array to be more random like : 1,1,0,1,0,0,1,0,0,0 and not 0,1,0,1,0,1,0,1

How can i do this without using Math.random of random()?

Thanx :)

it is little cumbersome but you can make an array of 999 values like – myArray = [0,1,1,0,0,1,1,0,1,0,1,0,1,0,1 .. and so on ..]; use myMathRand = my_date.getMilliseconds()/1000;

instead of directly using myMathRand, use myRandomNumber = myArray[myMathRand];

Let us know.

199 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 10+ items on Envato Market
+2 more
ivank says

Here you go man ;)

I remember we learned this stuff on Algorithms and Data structures subject I had 2 years ago, so I found algorithm in C++ and modified it. It’s your lucky day :D


var date:Date = new Date(); //we use date to get different seeds

//seeds
var m_w:uint = date.getSeconds()+1;     //must not be 0, so we add 1, that should work...
var m_z:uint = date.getMilliseconds()+1;  //same here..
//Marsaglia's MWC algorithm 
function GetUint():uint
{
    m_z = 36969 * (m_z & 65535) + (m_z >> 16);
    m_w = 18000 * (m_w & 65535) + (m_w >> 16);
    return (m_z << 16) + m_w;  
}
function GetUniform():Number
{
    var u:uint = GetUint();
    return (u + 1) * 2.328306435454494e-10;
}

//100 random numbers between 0 and 1 
for(var i=0; i<100; i++){
trace(GetUniform());
}

p.s.

don’t try to understand how it works, just use it :)

edit:

just use Math.round(GetUniform()); for 0 and 1

Ivan

by
by
by
by
by
by