473,473 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

help with random thingy

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*/
}
}

Dec 21 '06 #1
3 1161
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>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 21 '06 #2
Lee
du*****@gmail.com 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.
--

Dec 21 '06 #3
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.

Dec 21 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Rex_chaos | last post by:
Hi all, I am writing a project in C++. I am going to compile all my source to a library(.a). So I can only distribute the lib as well as the header file to my client. However, I don't quite...
3
by: Maxine | last post by:
Hi, I have been given this concept to turn into a full site. www.complete.ca/urlcanada/HTML I have recently been trying to design exclusively in CSS (I've taken the intro to CSS course via...
14
by: BL | last post by:
there's a sumthing like a scandisk tool in linux that will run during bootup when we have improper bootup. it looks sumthing like this: 46% i find the spinning thingy in the progression bar...
10
by: Johnny Snead | last post by:
Hey guys, Need help with this random sort algorithm private void cmdQuestion_Click(object sender, System.EventArgs e) { Random rnd = new Random(); //initialize rnd to new random object...
2
by: Martin Ho | last post by:
Hi Everyone, I have this code of Mersenne twister, which produces the random numbers, one of the fastest codes as far as I know to produce random numbers. Anyways, it's writen in c# and I need to...
23
by: Alvin | last post by:
Well, I'm developing a Tetris game in SDL, but when it comes to deciding the next block, I'm stuck. It's random, but when I try something like seeding the randomizer with the time, it won't update...
36
by: Cap'n Ahab | last post by:
I have used VB3 - VB6, so learning all this OO stuff is reasonably new to me (although I looked at Java a few years ago). Anyway, I thought I would write a small class to begin with, with a...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
4
by: UMstudent | last post by:
I'm working on this program for class where I have to an amount of numbers that are randomly picked from 0 - 50. So what I have done is say there are 20 slots in my array so I'm trying to generator...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.