Closed Thread Icon

Preserved Topic: name Randomizer (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=17882" title="Pages that link to Preserved Topic: name Randomizer (Page 1 of 1)" rel="nofollow" >Preserved Topic: name Randomizer <span class="small">(Page 1 of 1)</span>\

 
eyezaer
Lunatic (VI) Mad Scientist

From: the Psychiatric Ward
Insane since: Sep 2000

posted posted 10-03-2000 19:04

ok, i need to make a randomizer for about 18 names, i would like it to be a button, that when hit randomizes, and, to make things funner, when it is run the next time it will only display names that it hasent already displayed...ok?
i got the code from another post,
<script language="JavaScript">
function RandomText()
{
var RandNum = Math.random()*3;
if (RandNum <= 1) return "This is a name";
else if (RandNum <= 2) return "Fred";
else if (RandNum <= 3) return "Lulu";}
</script>

I really am gonna learn JS some day soon!

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 10-03-2000 20:54

Hmm. That code you have there will give you a random name, but avoiding previous names can be tough! You need about 3 arrays to do it; one contains all the names, one contains boolean (true or false) values explaining which ones have been used so far, and then each time the code is run you use that array in conjunction with the first array to create a *third* array, which only contains names that haven't been used yet, and then you choose from that.

But I gotta be honest with you, I don't like giving out code to someone who doesn't understand how it works, and is just going to cut and paste. I can do it if I must...

eyezaer
Lunatic (VI) Mad Scientist

From: the Psychiatric Ward
Insane since: Sep 2000

posted posted 10-03-2000 22:27

ah slime, your a good man... a little green for my tastes (i saw ya on your web cam), but
a good man non the less... so, i found these links on another page, http://www.dansteinman.com/dynduo
and, http://developer.netscape.com/docs/manuals/communicator/jsref/index.htm I was also
wondering where else i should go to learn about JS. Do you recommend any books,
ya know, those old things made out of paper that have text and static diagrams?

Das
Maniac (V) Inmate

From: Houston(ish) Texas
Insane since: Jul 2000

posted posted 10-03-2000 23:09

"JavaScript Bible" (IDG Books) is what I use. Very comprehensive.

"Dynamic HTML, The Definitive Reference" (O'Reilly) also has some good JS info.

Das
Maniac (V) Inmate

From: Houston(ish) Texas
Insane since: Jul 2000

posted posted 10-03-2000 23:12

Oh, and Slime, I cando it with one array <img border=0 align=absmiddle src="http://www.ozones.com/forum/smile.gif">

Edit: went ahead and wrote it. This is the source for a page demonstrating the solution I came up with. It "re-shuffles" after you've used every name once:

<--------snip------->
<html>
<head>
<title>Random names - demo</title>
<script language="JavaScript" type="text/javascript">
var nameslist = new Array();
function Rand(x) {
return Math.round(Math.random() * x);
}
function InitNames() {
nameslist[0] = "Alan";
nameslist[1] = "Bob";
nameslist[2] = "Charlie";
nameslist[3] = "Dan";
nameslist[4] = "Ed";
nameslist[5] = "Fred";
nameslist[6] = "----"; // make SURE you end the list with "----"
}
function RandName() {
var enditem = 0;
var target;
var ret;
if (nameslist[0] == "----") InitNames();
for (i = 0; i < nameslist.length; i++)
if (nameslist[i] != "----") enditem = i;
target = Rand(enditem);
ret = nameslist[target];
nameslist[target] = nameslist[enditem];
nameslist[enditem] = "----";
return ret;
}
</script>
</head>
<body bgcolor="#FFFFFF">
<script language="JavaScript" type="text/javascript">
InitNames();
for (k = 0; k < 3; k++) {
for (j = 0; j < 6; j++) {
document.writeln(RandName() + "<br>");
}
document.writeln(" ------ <br>");
}
</script>
</body>
</html>
<--------snip------->



[This message has been edited by Das (edited 03-10-2000).]

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 10-04-2000 00:49

As for books, grab "JavaScript: The Definitive Reference" (O'Reilly) before you get the DHTML one; it's what I learned from and literally explains everything. (The creator of JavaScript also recomends it.) It's not exactly cheap, but it's worth the money if you're serious about learning JavaScript.

Let me take a look at your code...

(woah, this is confusing, and yet really neat... still looking...)

Hmm... I'm not sure that works exactly as you planned it to. For one thing, the line nameslist[enditem] = "----"; is reduntant, because I believe enditem was defined as the first element in the array that *has* that value. Maybe I'm just confused; it's a very interesting little script there. And hey, if it works, then you might as well use it!

(btw, can I see this in action on the web?)

tea
Nervous Wreck (II) Inmate

From: Bern, Switzerland
Insane since: Aug 2000

posted posted 10-04-2000 00:52

learning how to use DTHML/JavaScript
i recommend two books from O'Reilly
"JavaScript, The Definitive Guide"
and
"Dynamic HTML, The Definitive Reference"

i am using both books myself.

cu,tea

tea
Nervous Wreck (II) Inmate

From: Bern, Switzerland
Insane since: Aug 2000

posted posted 10-04-2000 01:10

i am in good mood at this time here is
my code for you:

var names = new Array("Name 1","Name 2","Name 3","Name 4","Name 5","Name 6","Name 7","Name 8","Name 9","Name 10","Name 11");
var leng = names.length;
var tpos;
function display() {
if (!leng) return;
alert(names[tpos = Math.floor(Math.random()*leng--)]);
names[tpos] = names[leng];
}

call display as often as you like and you'll get another name at random each time.

cu,tea


tea
Nervous Wreck (II) Inmate

From: Bern, Switzerland
Insane since: Aug 2000

posted posted 10-04-2000 01:21

for a live test i uploaded a sample at:
http://www.tea.ch/test/random.htm

cu,tea

sorry used the file of my last example, you have to mouseover the ":Test Name" to get
the name alerted.



[This message has been edited by tea (edited 04-10-2000).]

eyezaer
Lunatic (VI) Mad Scientist

From: the Psychiatric Ward
Insane since: Sep 2000

posted posted 10-04-2000 03:44

great! ok, i guess that the good book, or best i should say, would be, JavaScript, The Definitive Guide... hmm... last time i was at borders w/ my friends i went off and started to look at that book... must be a sign!

Das
Maniac (V) Inmate

From: Houston(ish) Texas
Insane since: Jul 2000

posted posted 10-04-2000 06:51

Slime - actually, I do need the line you mentioned. The script first assigns enditem to the last item that doesn't have the value "----" (i.e. the last actual name). The line you mentioned overwrites the last item, shortening the list by one (so the same name won't appear again).

tea - your code seems to do the same thing as mine, except I'm using an end-of-data marker while you keep the current length of the array. Keeping the length is probably a bit simpler (oh, well).

A note: you can make your code "reshuffle", refreshing the list when it's already given each name once, by changing:
if (!leng) return;
to:
if (!leng) leng = names.length
if, eyezaer wants it that way, of course <img border=0 align=absmiddle src="http://www.ozones.com/forum/smile.gif">

(for the non-programmers, if(!leng) means the same thing as if(leng == 0). It's testing to see if the list is empty.)




[This message has been edited by Das (edited 04-10-2000).]

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 10-04-2000 19:30

Oh! Now I understand it! I guess I misread the != as ==. Very clever peice of code.

« BackwardsOnwards »

Show Forum Drop Down Menu