PDA

View Full Version : random numbers undefined.


redshift_1
12-07-2002, 03:18 PM
Okay. I may be being stupid here, but this script (designed to fill my weblog with random sites, natch) dosen't seem to work at all. The idea is to construct an IP address by combining 4 random numbers as the variable generatedIP, but the numbers aren't generating at all.

This originally used an array of about 50 sites and flicked randomly through the list just fine.

The random numbers seem to be the problem as they are permanantly undefined - what am i missing? also, is there any way to generate a random number that is already a string, since i'll probably have to turn them into strings to combine them like i want to.

And yes, i realise it's the most useless piece of js ever wrote.

...

<META HTTP-EQUIV=Refresh CONTENT="10">
<SCRIPT LANGUAGE="JavaScript">

var generatedIP;
var a = math.floor(math.random()*255); //a.#.#.#
var b = math.floor(math.random()*255); //#.b.#.#
var c = math.floor(math.random()*255); //#.#.c.#
var d = math.floor(math.random()*255); //#.#.#.d
var z = math.floor((math.random()*4)*2000); //random interval

generatedIP = a+"."+b+"."+c+"."+d; //generated IP address

function logSpoof() {
self.setTimeout("targetframe.location.href = generatedIP;",z);
}

// End -->
</SCRIPT>

there is some other stuff here. 'targetframe' is an iframe incase you wondered.

mordred
12-07-2002, 03:26 PM
It's the Math object you want to use, not the (undefined) math object. ;)

redshift_1
12-07-2002, 03:36 PM
*dies*

thanks a lot!

Although i've yet to see the damn thing generate a valid IP address :o

redshift_1
12-09-2002, 01:10 PM
As it turned out random IP adresses were a bad idea since in an hour i didn't see one valid IP found, but hey, that's random numbers for you. A much better way would be to use an inline dictionary or three and build random URLs (http://www.AdjectiveVerb.Domain if case you care).

Hurrah. except this dosen't work either. Obviously i'm missing something obvious (again) and natrually the arrays i've used are too small to currently be of much use, but this is my test model.


<!--
// strawberry flavoured URL generator redshift_1@hotmail.com

var adjective = "";
var noun = "";
var domain = "";
var waitTime = "";
var waitTimeSec = "";
var generatedURL = "";

adjectiveList = new Array();
adjectiveList[0] = "hot";
adjectiveList[1] = "cold";
adjectiveList[2] = "tall";
adjectiveList[3] = "short";
adjectiveList[4] = "power";

nounList = new Array();
nounList[0] = "dog";
nounList[1] = "cat";
nounList[2] = "computer";
nounList[3] = "world";
nounList[4] = "man";

domainList = new Array();
domainList[0] = "com";
domainList[1] = "org";
domainList[2] = "net";
domainList[3] = "co.uk";

currentSeconds = new Date();
randomSeed = currentSeconds.getSeconds();

var a = Math.round(Math.random(randomSeed)*adjectiveList.length);
if (a == adjectiveList.length)
{
a = 0
}

var b = Math.round(Math.random(randomSeed)*nounList.length);
if (b == nounList.length)
{
b = 0
}

var c = Math.round(Math.random(randomSeed)*domainList.length);
if (c == domainList.length)
{
c = 0
}

adjective = adjectiveList[a];
noun = nounList[b];
domain = domainList[c];

waitTime = Math.floor((Math.random(randomSeed)*5)*2000);
waitTimeSec = waitTime/1000;

generatedURL = "http:\\www."adjective+noun+"."+domain

function loadIP() {
self.setTimeout("targetframe.location.href = generatedURL;",waitTime);
}
//-->


As before i'm trying to open generatedURL in an IFRAME in the body of the document. This actually worked, but then i broke it and have no idea what i changed to trip it over.

I've just been staring at it for too long to even see what's wrong. It's probably a missing quote or something, but if you can see where i'm going wrong i'd be greatful.

Roelf
12-09-2002, 01:49 PM
Originally posted by redshift_1
generatedURL = "http:\\www." +adjective+noun+"."+domain


you missed a + sign

redshift_1
12-09-2002, 01:59 PM
ARGH! i knew it'd be something like that. I'm a JS chimp.

thanks v.much!