00:00
00:00
Newgrounds Background Image Theme

PalmVoe just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Foss: Custom Functions

6,847 Views | 106 Replies
New Topic Respond to this Topic

Response to Foss: Custom Functions 2005-11-13 23:24:29


page 4

im probly gonna stick with random(#).

Response to Foss: Custom Functions 2005-11-13 23:38:02


At 11/13/05 11:24 PM, ImpotentBoy2 wrote: im probly gonna stick with random(#).

thats wise. the actual algorithm, which im trying to derive, is a bit less time based. theyve combined a few different pieces to create something truly random. mine has its glitches still.

one small correction

function prandom():Number{
var whn:Date = new Date();
return (getTimer()%(whn.getMilliseconds()+1)/(whn
.getMilliseconds()+1;
}

function prandom2(num:Number):Number{
return Math.floor(prandom()*Math.floor(num));
}

thatll keep it from returning NaN when the milliseconds read zero...

BBS Signature

Response to Foss: Custom Functions 2005-11-14 08:35:41


At 11/13/05 09:42 PM, authorblues wrote: <random function>

A pretty good remake, but:

for (a=0; a<10; a++) {
trace(prandom2(10))
}
Output:

0
0
0
0
0
0
0
0
0
0

If you press CTRL+ENTER while the window is still open, a random number will appear but it will always be 10 of the same number.


Sup, bitches :)

BBS Signature

Response to Foss: Custom Functions 2005-11-14 08:39:13


At 11/14/05 08:35 AM, -liam- wrote: A pretty good remake, but:

unfortunately, as it is based on both the number of frames used, and the time, if you run it multiple times in the same frame, it will always output the same number. if i only knew how flash does it otherwise, id make myself useful, but until then, i guess this is the best i can do...


BBS Signature

Response to Foss: Custom Functions 2005-11-14 11:38:31


At 11/14/05 08:39 AM, authorblues wrote:
At 11/14/05 08:35 AM, -liam- wrote: A pretty good remake, but:
unfortunately, as it is based on both the number of frames used, and the time, if you run it multiple times in the same frame, it will always output the same number. if i only knew how flash does it otherwise, id make myself useful, but until then, i guess this is the best i can do...

pseudo random numbers are pre-generated and stored in a list: each time you call it, it choses the first item, then the next time choses the second item, then the third item until it gets to the end and loops back again

to make pseudo random numbers truly random (well not trully random, but near enough), you seed it with a value that constantly changes - i.e. milliseconds, ie. from the pseudo random number, you add or times or divide or whatever by the seed, and fit it between 0 and the number specified:

the pseudo random number list might be this:

5, 156, 324, 189, 15, 8 ,198, 153, 1564, 1894 though generally you would use numbers up the limits of an unsigned integer (and using integers for memory and speed)

then you might multiply to that the milliseconds: lets say its the fith time youve called it so youre using the pseudo number 15, the number of milliseconds is 15616, which becomes 234240, then if you entered 20 into the function, 234240%20 = 0;

heres an output from my pseudo seeded random number generator using a randomized list of numbers from 0 - 199, i used this loop:

var i:Number;
for(i=0;i<25;i++)
{
trace(prand(100));
}

with this result:

48
49
72
16
86
50
1
51
96
64
63
9
52
17
53
51
47
54
60
74
33
55
5
56
90

i tested the movie again and got this:

0
49
98
47
96
10
45
89
94
14
89
57
43
67
92
20
13
41
4
43
9
31
59
90
39

the only time ive used random() was when randomizing the list of numbers, which woud usually be pre defined but i couldnt be bothered to stit their typing a randomized list of 200 numbers so you know.... lol

Response to Foss: Custom Functions 2005-11-14 13:03:53


function rgbToHex(r:Number, g:Number, b:Number):String {
var q:String = r.toString(16);
var w:String = g.toString(16);
var e:String = b.toString(16);
q<10 ? q="0"+q : 0, w<10 ? w="0"+w : 0, e<10 ? e="0"+e : 0;
return q+w+e;
}

trace(rgbToHex(255, 0, 0)) = FF0000

if you want the output to be 0xFF0000 then change the last line (return.. ) to:

return "0x"+q+w+e;


Sup, bitches :)

BBS Signature

Response to Foss: Custom Functions 2005-11-14 13:24:15


At 11/14/05 11:38 AM, dELta_Luca wrote: pseudo random numbers are pre-generated and stored in a list

var randSeed:Array = new Array();
_root.onEnterFrame=function(){
if(randSeed.length<500){
var pushedNum:Number = prandomgen();
randSeed.push(pushedNum);
};
}

function prandomgen():Number{
var whn:Date = new Date();
return (getTimer()%(1+whn.getMilliseconds()))/(1+
whn.getMilliseconds());
}

function prandom():Number{
return Number(randSeed.pop());
}

function prandom2(num:Number):Number{
return Math.floor(prandom()*Math.floor(num));
}

alright, i think this should work. the only drawback is that you have to wait for 500 frames to go by for the list to fill up, but this should solve that pesky problem. thanks for answering my question, dELta. you know all that math knowledge turns me on, right?


BBS Signature

Response to Foss: Custom Functions 2005-11-14 13:26:55


At 11/14/05 01:24 PM, authorblues wrote: var randSeed:Array = new Array();
_root.onEnterFrame=function(){
if(randSeed.length<500){
var pushedNum:Number = prandomgen();
randSeed.push(pushedNum);
};
}

var randSeed:Array = new Array();
_root.onEnterFrame=function(){
randSeed.length<500?randSeed.push(prandomg
en()) : 0;
}

A lot easier, no?


Sup, bitches :)

BBS Signature

Response to Foss: Custom Functions 2005-11-14 13:29:27


At 11/14/05 01:26 PM, -liam- wrote: var randSeed:Array = new Array();
_root.onEnterFrame=function(){
randSeed.length<500?randSeed.push(prandomg
en()) : 0;
}

your ability to tear apart my programming, however, does NOT turn me on.
but yes, you are absolutely correct. now i expect NO one to ever use my random class,
but it was fun to write up...


BBS Signature

Response to Foss: Custom Functions 2005-11-14 13:40:12


At 11/14/05 01:03 PM, -liam- wrote: trace(rgbToHex(255, 0, 0)) = FF0000

if you want the output to be 0xFF0000 then change the last line (return.. ) to:

return "0x"+q+w+e;

i dont like string representation :( r << 16 | b<<8 | g however :)

Response to Foss: Custom Functions 2005-11-14 13:44:10


At 11/14/05 01:40 PM, dELta_Luca wrote: i dont like string representation :( r << 16 | b<<8 | g however :)

Binary is scary :'(


Sup, bitches :)

BBS Signature

Response to Foss: Custom Functions 2005-11-14 13:53:50


At 11/14/05 01:44 PM, -liam- wrote:
At 11/14/05 01:40 PM, dELta_Luca wrote: i dont like string representation :( r << 16 | b<<8 | g however :)
Binary is scary :'(

? lol its not that complicated:

r,g and b are values from 0-255, represented in binary this is 0 - 11111111
so whats the best way to represent 3 numbers as 1 number while still being able to extra each part? join them together:
0xRRGGBB = RRRRRRRRGGGGGGGGBBBBBBBB
0xff808f = 111111110111000001111111 =
11111111 << 16 + 01110000 << 8 + 011111111 =
0xff << 16 + 0x80 << 8 + 0x8f =
255 << 16 + 128 << 8 + 143 =

but bitwise OR ( | ) is faster then ( + ) so we use that instead
255 << 16 | 128 << 8 | 143 ~
R << 16 | G << 8 | B

then to extract

rgb = 0xRRGGBB
r = rgb>>16;
gb = rgb-(r<<16);
g = gb>>8;
b = gb-(g<<8);

rgb = 0xff808f
rgb >> 16 = 0xff = 255
gb = rgb-(r<<16) = 0xff808f - (0xff<<16) = 0xff808f - 0xff0000 = 0x808f
g = gb>>8 = 0x808f >> 8 = 0x80 = 128
b = gb-(g<<8) = 0x808f - 0x80 << 8 = 0x808f - 0x8000 = 0x8f = 143

Response to Foss: Custom Functions 2005-11-24 15:37:40


At 11/14/05 01:53 PM, -dELta- wrote: ? lol its not that complicated:

Then you divulge into a barrage of numbers, roffle :)

function wordCount(str:String):Number {
num = str.length>0 ? 1 : 0;
for (a=0; a<str.length; a++) {
if (str.charAt(a) == " " && str.charAt(a-1) != " " && str.charAt(a+1) != " " && a != str.length-1) {
num++;
}
}
return num;
}

Usage:

trace(wordCount("There are seven words in this sentence.")); //7


Sup, bitches :)

BBS Signature

Response to Foss: Custom Functions 2005-11-25 11:45:33


function averageLength(str:String):Number {
return (str.length-countSpaces(str)+1)/wordCount(
str);
}
function countSpaces(str:String):Number {
num = str.length>0;
for (a=0; a<str.length; a++) {
if (str.charAt(a) == " ") {
num++;
}
}
return num;
}

To be used with the wordCount function.. ejemplo.


Sup, bitches :)

BBS Signature

Response to Foss: Custom Functions 2005-11-29 14:29:47


Just playing with the arguments variable in functions, use this to draw a closed shape..

function drawShape():MovieClip {
var d:Number = _root.getNextHighestDepth();
mc = _root.createEmptyMovieClip("mc"+d, d);
mc.lineStyle(2, 0, 100);
mc.moveTo(arguments[0][0], arguments[0][1]);
for (a=1; a<arguments.length; a++) {
mc.lineTo(arguments[a][0], arguments[a][1]);
}
mc.lineTo(arguments[0][0], arguments[0][1]);
return mc;
}

Usage:

mc = drawShape([10, 10], [10, 390], [540, 390], [540, 10]);


Sup, bitches :)

BBS Signature

Response to Foss: Custom Functions 2005-11-29 16:12:41


At 11/29/05 02:29 PM, -liam- wrote: Just playing with the arguments variable in functions, use this to draw a closed shape..

function drawShape():MovieClip {
var d:Number = _root.getNextHighestDepth();
mc = _root.createEmptyMovieClip("mc"+d, d);
mc.lineStyle(2, 0, 100);
mc.moveTo(arguments[0][0], arguments[0][1]);
for (a=1; a<arguments.length; a++) {
mc.lineTo(arguments[a][0], arguments[a][1]);
}
mc.lineTo(arguments[0][0], arguments[0][1]);
return mc;
}

Usage:

mc = drawShape([10, 10], [10, 390], [540, 390], [540, 10]);
function drawShape():MovieClip {
var d:Number = _root.getNextHighestDepth();

var mc:MovieClip = _root.createEmptyMovieClip("mc"+d, d);

mc.lineStyle(2, 0, 100);

mc.moveTo(arguments[0], arguments[1]);
for (a=2; a<arguments.length; a+=2) {
mc.lineTo(arguments[a], arguments[a+1]);

}

mc.lineTo(arguments[0], arguments[1]);

return mc;
}

Usage:

mc = drawShape(10, 10, 10, 390,540, 390, 540, 10);
//drawShape(x1,y1,x2,y2,x3,y3,x4,y4.....xn
,yn);

Response to Foss: Custom Functions 2005-11-29 16:15:35


At 11/29/05 04:12 PM, -dELta- wrote: mc = drawShape(10, 10, 10, 390,540, 390, 540, 10);
//drawShape(x1,y1,x2,y2,x3,y3,x4,y4.....xn
,yn);

the version where the coordinates are stored as an array is much easier to debug.
thats just my opinion. but, if you want to rewrite it, by all means...


BBS Signature