| You are here: DEVPPL ‹ Forum ‹ Programming ‹ JavaScript Forum ‹ Script-archive |
NOTIFICATIONS
|
|
|||||||||||||||
Login |
Random Is Not Random
1 post
• Page 1 of 1
0
Random Is Not Random
This script is just to show you how JavaScript's Math.random function is not at all very random
According to the code, the words should be moving in random directions. However, notice what actually happens. Oh yeah, that script will make your computer lag a lot (i made it when i was bored)
- Code: Select all
<script>
var switchVal = 1;
var count = 0;
var max = 800;
var interval = 3000;
function loadZzzs()
{
var xy = 0;
while(xy < max)
{
document.write('<div class="noMove" style="z-index: '+xy+'; position: absolute; top: '+xy+' ; left: '+xy+' ;">Zzz...</div>');
document.write('<div style="z-index: '+xy+'; position: absolute; top: '+xy+' ; left: '+(xy + Math.random()*100)+' ;">Zzz...</div>');
document.write('<div style="z-index: '+xy+'; position: absolute; top: '+xy+' ; left: '+(xy - Math.random()*100)+' ;">Zzz...</div>');
xy+=5;
var temp = max - xy;
document.write('<div class="noMove" style="z-index: '+xy+'; position: absolute; top: '+xy+' ; left: '+temp+' ;">Zzz...</div>');
document.write('<div class="noMove" style="z-index: '+xy+'; position: absolute; top: '+xy+' ; left: '+(max/2)+' ;">Zzz...</div>');
document.write('<div class="noMove" style="z-index: '+xy+'; position: absolute; top: '+(max/2)+' ; left: '+temp+' ;">Zzz...</div>');
document.write('<div style="z-index: '+xy+'; position: absolute; top: '+xy+' ; left: '+(temp + Math.random()*100)+' ;">Zzz...</div>');
document.write('<div style="z-index: '+xy+'; position: absolute; top: '+xy+' ; left: '+(temp - Math.random()*100)+' ;">Zzz...</div>');
document.write('<div style="z-index: '+xy+'; position: absolute; top: '+(max/2 - Math.random()*100)+' ; left: '+(temp)+' ;">Zzz...</div>');
document.write('<div style="z-index: '+xy+'; position: absolute; top: '+(max/2 + Math.random()*100)+' ; left: '+(temp)+' ;">Zzz...</div>');
document.write('<div style="z-index: '+xy+'; position: absolute; top: '+xy+' ; left: '+((max/2) + Math.random()*100)+' ;">Zzz...</div>');
document.write('<div style="z-index: '+xy+'; position: absolute; top: '+xy+' ; left: '+((max/2) - Math.random()*100)+' ;">Zzz...</div>');
if((Math.random()*15) < 2)
{
var fontSize = "35px";
document.write('<div style="z-index: '+xy+'; position: absolute; top: '+xy+' ; left: '+(temp + Math.random()*200)+' ; font-size: '+fontSize+';">Zzz...</div>');
document.write('<div style="z-index: '+xy+'; position: absolute; top: '+xy+' ; left: '+(temp - Math.random()*200)+' ;font-size: '+fontSize+';">Zzz...</div>');
document.write('<div style="z-index: '+xy+'; position: absolute; top: '+(max/2 - Math.random()*200)+' ; left: '+(temp)+' ;font-size: '+fontSize+';">Zzz...</div>');
document.write('<div style="z-index: '+xy+'; position: absolute; top: '+(max/2 + Math.random()*200)+' ; left: '+(temp)+' ;font-size: '+fontSize+';">Zzz...</div>');
document.write('<div style="z-index: '+xy+'; position: absolute; top: '+xy+' ; left: '+((max/2) + Math.random()*200)+' ;font-size: '+fontSize+';">Zzz...</div>');
document.write('<div style="z-index: '+xy+'; position: absolute; top: '+xy+' ; left: '+((max/2) - Math.random()*200)+' ;font-size: '+fontSize+';">Zzz...</div>');
document.write('<div style="z-index: '+xy+'; position: absolute; top: '+xy+' ; left: '+(xy + Math.random()*200)+' ;font-size: '+fontSize+';">Zzz...</div>');
document.write('<div style="z-index: '+xy+'; position: absolute; top: '+xy+' ; left: '+(xy - Math.random()*200)+' ;font-size: '+fontSize+';">Zzz...</div>');
}
if((Math.random()*15) < 2)
{
var fontSize = "50px";
var color = "rgb(";
var red = Math.round(Math.random()*254);
var green = Math.round(Math.random()*254);
var blue = Math.round(Math.random()*254);
color += red + ",";
color += green + ",";
color += blue + ")";
document.write('<div style="z-index: '+xy+'; position: absolute; top: '+xy+' ; left: '+(temp + Math.random()*200)+' ; font-size: '+fontSize+'; color: '+color+';">Zzz...</div>');
document.write('<div style="z-index: '+xy+'; position: absolute; top: '+xy+' ; left: '+(temp - Math.random()*200)+' ;font-size: '+fontSize+';color: '+color+';">Zzz...</div>');
document.write('<div style="z-index: '+xy+'; position: absolute; top: '+(max/2 - Math.random()*200)+' ; left: '+(temp)+' ;font-size: '+fontSize+';color: '+color+';">Zzz...</div>');
document.write('<div style="z-index: '+xy+'; position: absolute; top: '+(max/2 + Math.random()*200)+' ; left: '+(temp)+' ;font-size: '+fontSize+';color: '+color+';">Zzz...</div>');
document.write('<div style="z-index: '+xy+'; position: absolute; top: '+xy+' ; left: '+((max/2) + Math.random()*200)+' ;font-size: '+fontSize+';color: '+color+';">Zzz...</div>');
document.write('<div style="z-index: '+xy+'; position: absolute; top: '+xy+' ; left: '+((max/2) - Math.random()*200)+' ;font-size: '+fontSize+';color: '+color+';">Zzz...</div>');
document.write('<div style="z-index: '+xy+'; position: absolute; top: '+xy+' ; left: '+(xy + Math.random()*200)+' ;font-size: '+fontSize+';color: '+color+';">Zzz...</div>');
document.write('<div style="z-index: '+xy+'; position: absolute; top: '+xy+' ; left: '+(xy - Math.random()*200)+' ;font-size: '+fontSize+';color: '+color+';">Zzz...</div>');
}
}
}
function flashBody()
{
var color = "rgb(";
var red = Math.round(Math.random()*254);
var green = Math.round(Math.random()*254);
var blue = Math.round(Math.random()*254);
changeColor(red, green, blue, document.body);
}
function moveZzzs()
{
count ++;
if(count > 300)
{
count = 0;
if(switchVal == 1)
{
switchVal = 0;
}
else
{
switchVal = 1;
}
}
div = document.getElementsByTagName('div');
for(i=0; i<div.length; i++)
{
if(div[i].className != "noMove")
{
var b = div[i].style;
var left = b.left.replace('px', '');
var top = b.top.replace('px', '');
if(Math.round(Math.random()) == switchVal)
{
left = left+Math.round(Math.random()*30);
top = top+Math.round(Math.random()*30);
}
else
{
left = left-Math.round(Math.random()*30);
top = top-Math.round(Math.random()*30);
}
if(left > 0 && left < max)
{
b.left = left;
}
if(top > 0 && top < max)
{
b.top = top;
}
}
}
}
var oldRed = 255;
var oldGreen = 255;
var oldBlue = 255;
var goalRed = 0;
var goalGreen = 0;
var goalBlue = 0;
var redChange = 0;
var greenChange = 0;
var blueChange = 0;
var changeColorInterval;
var changeColorCount = 0;
var smoothness = 10;
var colorObj;
function changeColor(red, green, blue, obj)
{
colorObj = obj;
goalRed = red;
goalGreen = green;
goalBlue = blue;
redChange = oldRed - red;
greenChange = oldGreen - green;
blueChange = oldBlue - blue;
redChange /= smoothness;
greenChange /= smoothness;
blueChange /= smoothness;
redChange = Math.round(redChange);
greenChange = Math.round(greenChange);
blueChange = Math.round(blueChange);
changeColorInterval = window.setInterval('changeColorExe()', (Math.round((interval * 0.9)/smoothness)));
}
function changeColorExe()
{
if(changeColorCount < (smoothness+1))
{
changeColorCount++;
oldRed += redChange;
oldGreen += greenChange;
oldBlue += blueChange;
var color = "rgb(";
color += oldRed + ",";
color += oldGreen + ",";
color += oldBlue + ")";
document.body.style.backgroundColor = color;
}
else
{
changeColorCount = 0;
oldRed = goalRed;
oldGreen = goalGreen;
oldBlue = goalBlue;
var color = "rgb(";
color += oldRed + ",";
color += oldGreen + ",";
color += oldBlue + ")";
document.body.style.backgroundColor = color;
clearInterval(changeColorInterval);
}
}
</script>
<body style="margin: 0px 0px 0px 0px;">
<script>
loadZzzs();
flashBody();
window.setInterval('flashBody()', interval);
window.setInterval('moveZzzs()', 100);
</script>
</body>
According to the code, the words should be moving in random directions. However, notice what actually happens. Oh yeah, that script will make your computer lag a lot (i made it when i was bored)
- HotNoob
- Reputation: 0
- Posts: 169
- Joined: Sun May 02, 2010 2:38 am
- Highscores: 0
- Arcade winning challenges: 0
|
|