473,372 Members | 1,040 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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=javascript 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="Javascript">
// <!--
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 8794
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/>");
}

--

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.google.c om...
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=javascript 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="Javascript">
// <!--
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************************@news-server.nc.rr.com>,
seen in news:comp.lang.javascript, Dan Brussee <dbrussee@NOTbetterwaycom
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*Math.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.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.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
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...
6
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
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...
4
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. ...
4
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...
0
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...
4
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...
10
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...
34
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.