help with random thingy

D

duffint

Hi there,

I have this script that I need some direction in; it's mangled my head
a bit.

I want to be able to stick links around six random words; these links
are then popup ads.

I saw kind of a similar thing on a site a while ago.

Basically, I have these 4 ads that I want to be able to turn on in a
random order, for example: The page loads and the first, third, fifth
and sixth predetermined words are active ad popups.

The problem I'm having is with the turning on randomly part.

If someone could please say whether I'm going in the right direction at
all for starters, and how I would check if the a number has already
come up in the loop.

Thank you ever so much, and happy christmas...

This is what I have so far:



function turn_ads_on() {

var add_01 = false;
var add_02 = false;
var add_03 = false;
var add_04 = false;
var add_05 = false;
var add_06 = false;

var which_ad = 0;

for (i=0; i<=3; i++1) {

which_ad = (Math.random() * 6) +1
which_ad = parseInt(which_ad, 10)


if (which_ad==1) {
add_01 == true;
/*something to dissallow 1 from comin up again*/
}

if (which_ad==2) {
add_02 == true;
/*something to dissallow 2 from comin up again*/
}

if (which_ad==3) {
add_03 == true;
/*something to dissallow 3 from comin up again*/
}

if (which_ad==4) {
add_04 == true;
/*something to dissallow 4 from comin up again*/
}

if (which_ad==5) {
add_05 == true;
/*something to dissallow 5 from comin up again*/
}

if (which_ad==6) {
add_06 == true;
/*something to dissallow 6 from comin up again*/
}
}
 
L

Lee

(e-mail address removed) said:
Hi there,

I have this script that I need some direction in; it's mangled my head
a bit.

I want to be able to stick links around six random words; these links
are then popup ads.

I saw kind of a similar thing on a site a while ago.

Basically, I have these 4 ads that I want to be able to turn on in a
random order, for example: The page loads and the first, third, fifth
and sixth predetermined words are active ad popups.

The problem I'm having is with the turning on randomly part.

If someone could please say whether I'm going in the right direction at
all for starters, and how I would check if the a number has already
come up in the loop.

Thank you ever so much, and happy christmas...

This is what I have so far:
which_ad = (Math.random() * 6) +1
which_ad = parseInt(which_ad, 10)

First of all, don't indent your code so much if you're going to
post it to a newsgroup. It's hard to read code when the lines wrap.

Second, parseInt() is for parsing integers out of strings.
which_ad is a number, so, behind the scenes Javascript has
to convert it from a number to a string in order for parseInt
to convert it back again:

which_ad = Math.floor(Math.random()*6)+1;

Third, you could shorten your code considerably by using an
array instead of six separate variables, but I'll leave it
to you to make that enhancement.

You should also read this newsgroup's FAQ. It mentions
the indentation thing and points to some excellent information
about random selection and "dealing", which would further
simplify your code.

http://jibbering.com/faq/index.html

As for your algorithm, the big problem seems to be that you've
decided in advance that your loop will iterate four times,
when you really don't know how many tries it will take until
you've selected four different numbers. You might want to
keep a counter variable and increment it each time you choose
a new number, and stop the loop when you have chosen 4.

As for detecting whether a number has been chosen already,
just look to see if the value is true. If so, you don't
want to choose that one, and should just continue to another
pass through the loop. If it's false, mark it true and also
increment your count before looping again (remembering to
stop looking if you have enough.


--
 
E

Evertjan.

wrote on 21 dec 2006 in comp.lang.javascript:
Hi there,

I have this script that I need some direction in; it's mangled my head
a bit.

I want to be able to stick links around six random words; these links
are then popup ads.

I saw kind of a similar thing on a site a while ago.

Basically, I have these 4 ads that I want to be able to turn on in a
random order, for example: The page loads and the first, third, fifth
and sixth predetermined words are active ad popups.

The problem I'm having is with the turning on randomly part.

If someone could please say whether I'm going in the right direction
at all for starters, and how I would check if the a number has already
come up in the loop.

Thank you ever so much, and happy christmas...

This is what I have so far:



function turn_ads_on() {

var add_01 = false;
var add_02 = false;
var add_03 = false;
var add_04 = false;
var add_05 = false;
var add_06 = false;

var which_ad = 0;

for (i=0; i<=3; i++1) {


i++1 ?????????
which_ad = (Math.random() * 6) +1
which_ad = parseInt(which_ad, 10)


if (which_ad==1) {
add_01 == true;
/*something to dissallow 1 from comin up
again*/
}

if (which_ad==2) {
add_02 == true;
/*something to dissallow 2 from comin up
again*/
}

if (which_ad==3) {
add_03 == true;
/*something to dissallow 3 from comin up
again*/
}

if (which_ad==4) {
add_04 == true;
/*something to dissallow 4 from comin up
again*/
}

if (which_ad==5) {
add_05 == true;
/*something to dissallow 5 from comin up
again*/
}

if (which_ad==6) {
add_06 == true;
/*something to dissallow 6 from comin up
again*/
}
}

Try:

<script type='text/javascript'>

function turn_ads_on() {

var numberToReturn = 3;
var numberOfChoices = 6;
var adArray = [];
var resultArray = [];

while (resultArray.length < numberToReturn) {
whichAd = Math.floor(Math.random() * numberOfChoices);
if (!adArray[whichAd]) {
resultArray[resultArray.length] = whichAd + 1;
adArray[whichAd] = true;
};
};
return resultArray;
};

alert(turn_ads_on())

</script>
 
T

tim

Hi,

Thanks a million lads; I'm going to get up early tomorrow and decipher
your reply's as undoubtedly you can tell this is all quite knew to me,
a thing ye've probably read umpteen million times.

Thanks, Happy Christmas.

Tim D.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,246
Latest member
softprodigy

Latest Threads

Top