68 posts
  • Bought between 100 and 499 items
  • Exclusive Author
  • Has been a member for 7-8 years
  • Netherlands
  • Sold between 1 000 and 5 000 dollars
botervet says

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

If yes… how can i do this?

Thanx

3674 posts Community Moderator
  • Has been a member for 5-6 years
  • Contributed a Tutorial to a Tuts+ Site
  • Netherlands
  • Community Moderator
  • Envato Studio (Microlancer) Beta Tester
  • Sold between 10 000 and 50 000 dollars
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Exclusive Author
+4 more
Joost Volunteer 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
  • Author had a File in an Envato Bundle
  • Has been a member for 5-6 years
  • Sold between 50 000 and 100 000 dollars
  • Exclusive Author
  • Referred between 10 and 49 users
  • Bought between 50 and 99 items
FlashTang says
do you mean random index ?
yourArray[uint(Math.random()*yourArray.length)]
68 posts
  • Bought between 100 and 499 items
  • Exclusive Author
  • Has been a member for 7-8 years
  • Netherlands
  • Sold between 1 000 and 5 000 dollars
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 :)

1676 posts Envato Elite Author
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Author had a Free File of the Month
  • Exclusive Author
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Europe
  • Has been a member for 4-5 years
  • Referred between 10 and 49 users
+1 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
  • Bought between 100 and 499 items
  • Exclusive Author
  • Has been a member for 7-8 years
  • Netherlands
  • Sold between 1 000 and 5 000 dollars
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
  • Exclusive Author
  • Won a Competition
  • Most Wanted Bounty Winner
  • Sold between 10 000 and 50 000 dollars
  • Philippines
  • Bought between 10 and 49 items
  • Has been a member for 4-5 years
+1 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
  • Bought between 100 and 499 items
  • Exclusive Author
  • Has been a member for 7-8 years
  • Netherlands
  • Sold between 1 000 and 5 000 dollars
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
  • Exclusive Author
  • Won a Competition
  • Most Wanted Bounty Winner
  • Sold between 10 000 and 50 000 dollars
  • Philippines
  • Bought between 10 and 49 items
  • Has been a member for 4-5 years
+1 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
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Referred between 1 and 9 users
  • Serbia
  • Sold between 1 000 and 5 000 dollars
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