By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,156 Members | 962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,156 IT Pros & Developers. It's quick & easy.

random text script, BUT no repeating

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.