473,657 Members | 2,432 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

random text script, BUT no repeating

Hello--

I would like to display a different line of text (32 different
messages) on refresh, BUT with no repeats.

The script I am currently using is:

<script language=javasc ript type=text/javascript>
<!--
a = Math.floor(Math .random() * 32);
switch (a){
case 0 :
document.write( "msg 1 here");
break;
case 1 :
document.write( "msg 2 here");
break;
}
// etc., up to 32 cases
//-->
</script>

I notice that many random text scripts use an array to store the text
messages, and was thinking that incorporating a line in the script
where the array value that was most recently accessed would be
deleted, would make its subsequent selection impossible, thereby
solving the no-repeats.

In the script below, can one suggest where the delete line would fit
in and what its syntax would be?

thank you in advance!
JF

<SCRIPT LANGUAGE="Javas cript">
// <!--
function text() {
};

text = new text();
number = 0;

// textArray
text[number++] = "Random text string 1."
text[number++] = "Random text string 2."
text[number++] = "Random text string 3."
text[number++] = "Random text string 4."
text[number++] = "Random text string 5."
// keep adding items here...

increment = Math.floor(Math .random() * number);

document.write( text[increment]);

// where does delete text[number++] fit in?

//--></SCRIPT>
Jul 20 '05 #1
3 8818
In article <be************ **************@ posting.google. com>,
je***********@y ahoo.ca says...
Hello--

I would like to display a different line of text (32 different
messages) on refresh, BUT with no repeats.


Think of this is a sort of card shuffling routine. You have 32 cards and
want them randomized. Obviously, not duplicates.

A common way of doing this in gaming is to take the array of cards and
just mix up their values. Then go through the array one at a time as if
they were NOT shuffled. Psuedo-code follows:

var a;
var b;
var tmp;
for (var y = 0; y < 10; y++) { // repeat shuffle y times
for (var x = 0; x < 32; x++) {
a = math.random(32) ; // cant remember the method here... sorry.
b = math.random(32) ;
tmp = ary(a);
ary(a) = ary(b);
ary(b) = tmp;
}
}

for (var x = 0; x < 32; x++) {
document.write( "Item " + x + ": " + ary(x) + "<br/>");
}

--

Remove NOT from email address to reply. AntiSpam in action.
Jul 20 '05 #2
You want to do this every time the page is refreshed?
Unless this is a framed page, it's not clear how the page will know what the
last-used line was...

tim.


"Jennie Friesen" <je***********@ yahoo.ca> wrote in message
news:be******** *************** ***@posting.goo gle.com...
Hello--

I would like to display a different line of text (32 different
messages) on refresh, BUT with no repeats.

The script I am currently using is:

<script language=javasc ript type=text/javascript>
<!--
a = Math.floor(Math .random() * 32);
switch (a){
case 0 :
document.write( "msg 1 here");
break;
case 1 :
document.write( "msg 2 here");
break;
}
// etc., up to 32 cases
//-->
</script>

I notice that many random text scripts use an array to store the text
messages, and was thinking that incorporating a line in the script
where the array value that was most recently accessed would be
deleted, would make its subsequent selection impossible, thereby
solving the no-repeats.

In the script below, can one suggest where the delete line would fit
in and what its syntax would be?

thank you in advance!
JF

<SCRIPT LANGUAGE="Javas cript">
// <!--
function text() {
};

text = new text();
number = 0;

// textArray
text[number++] = "Random text string 1."
text[number++] = "Random text string 2."
text[number++] = "Random text string 3."
text[number++] = "Random text string 4."
text[number++] = "Random text string 5."
// keep adding items here...

increment = Math.floor(Math .random() * number);

document.write( text[increment]);

// where does delete text[number++] fit in?

//--></SCRIPT>

Jul 20 '05 #3
JRS: In article <MP************ ************@ne ws-server.nc.rr.co m>,
seen in news:comp.lang. javascript, Dan Brussee <dbrussee@NOTbe tterwaycom
puting.com> posted at Mon, 30 Jun 2003 01:06:41 :-
In article <be************ **************@ posting.google. com>,
je***********@ yahoo.ca says...
Hello--

I would like to display a different line of text (32 different
messages) on refresh, BUT with no repeats.


Think of this is a sort of card shuffling routine. You have 32 cards and
want them randomized. Obviously, not duplicates.

A common way of doing this in gaming is to take the array of cards and
just mix up their values. Then go through the array one at a time as if
they were NOT shuffled. Psuedo-code follows:

var a;
var b;
var tmp;
for (var y = 0; y < 10; y++) { // repeat shuffle y times
for (var x = 0; x < 32; x++) {
a = math.random(32) ; // cant remember the method here... sorry.
b = math.random(32) ;
tmp = ary(a);
ary(a) = ary(b);
ary(b) = tmp;
}
}

for (var x = 0; x < 32; x++) {
document.write( "Item " + x + ": " + ary(x) + "<br/>");
}


Pseudo.

Good idea, bad algorithm. See FAQ 4.22, both for
function Random(x) { return Math.floor(x*Ma th.random()) }
// generates random integer in 0 .. x-1
and for a link to efficient dealing and shuffling.

For N items, there are N! possible orders, so for a good shuffle the
operation must have [a multiple of] N! possible outcomes, all equi-
probable. For this it is both necessary and sufficient to call
Random(N) ... Random(2 or 1).

With, therefore, a little subtlety in your x loop, the y loop is not
needed.

function Shuffle(Q) { var R, T, J
for (J=Q.length-1 ; J>0 ; J--)
{ R=Random(J+1) ; T=Q[J] ; Q[J]=Q[R] ; Q[R]=T }
return Q }

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MSIE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #4

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

Similar topics

5
3103
by: Luke | last post by:
Hello, I am the administrator for http://www.nickberg.org - Nick is the man who was recently beheaded in Iraq. He was a very ingenious man - once for my birthday had gave me a small box wrapped in duck tape with a few diodes and two wires sticking out. When attaching the wires to your skin the diodes would light up in a random configuration. (he later modified this into a skin resistance
6
3519
by: Olly | last post by:
I've found a basic script, however I also need to add alt and title attributes as well, how would I go about doing this? Here's the script I found: Thanks <script language="JavaScript"> <!-- /*
4
16767
by: Phillo | last post by:
Hello, I'm new at Javascript, and have written a script for a series of random roll-over button images, but one thing I would like to add is a function that checks to make sure that there are no duplicates in the randomly generated variables that choose the pictures. Can anyone give me a hand with this? One other thing I can't seem to figure out is how to manage the "onLoad" aspect of caching my roll-over images (DW has locked the...
4
2748
by: tshad | last post by:
I am trying to set up an Image authorization where you type in the value that is in a picture to log on to our site. I found a program that is supposed to do it, but it doesn't seem to work. It should put a blue and yellow box on the page with "This is a test" as part of the picture. But what I get is a broken Gif. The other problem is that I can't view the source???? The view source is disabled for this page. What causes this?
4
3078
by: Kim | last post by:
Random image downloader for specified newsgroup. Hi I'm writing a small script that will download random images from a specified newsgroup. I've imported yenc into the script but I can't open the image or save it. This is my first script so be gentle! Heres the script ####random group downloader#### import nntplib import string, random import mimetools import StringIO
0
1524
by: Kim | last post by:
Random image downloader for specified newsgroup. Hi I'm writing a small script that will download random images from a specified newsgroup. I've imported yenc into the script but I can't open the image or save it. This is my first script so be gentle! Heres the script ####random group downloader#### import nntplib import string, random import mimetools import StringIO
4
2353
by: Christina | last post by:
A project I am working on requires 5 random popup windows that rotate. A cookie is set each time one of the popups occurs with a 5 day expire period. The visitor isn't supposed to see the same popup within that 5 day period, so it needs to rotate to another popup. I have changed it around several times (setCookie calls randomWin and vice versa) but now have brain burnout. I'm probably complicating it - a bad habit I have. My randoms...
10
3400
by: lifity | last post by:
hi, i am stuck on part of my project and very new to VB. i had cut my picture into 6 equals part but need to display them randomly, without repeating the same part of the pic again, in random sequence. eg, pic1,pic2,pic3,pic4,pic5,pic6 to ==> pic3,pic2,pic6,pic1,pic5,pic4 below are part of my code Dim theImage(0 To 7) As Object Dim myArray(0 To 7) As Object
34
4100
by: Johannes Baagoe | last post by:
About Math.random(), ECMA 262 just says "Returns a number value with positive sign, greater than or equal to 0 but less than 1, chosen randomly or pseudo randomly with approximately uniform distribution over that range, using an implementation-dependent algorithm or strategy." This is not good enough for my purposes (to generate identifiers with sufficient entropy, say 256 bits or so, to effectively preclude any chance of any two calls...
0
8395
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7330
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.