Advanced Search

Results 1 to 3 of 3

Thread: Random name that does not repeat

  1. #1
    Join Date
    Oct 2012
    Location
    England
    Posts
    88
    Thanks
    24
    Thanked 2 Times in 1 Post

    Default Random name that does not repeat

    Hi,

    I have been working on this code here, it's pretty simple and it allows me to choose a child's name at random.

    <html>
    <head>
    <script language="JAVASCRIPT">



    <!--

    function pupilsname() {
    pic = new Array();
    pic = new Array();
    pic[0] = "Adam Arnold";
    pic[1] = "Brenda Beeston";
    pic[2] = "Carl Carlton";
    pic[3] = "Deborah Daybrook";
    pic[4] = "Eric Eastwood";
    pic[5] = "Francis Findern";
    pic[0] = "Greg Grantham";
    pic[1] = "Hilda Heanor";
    pic[2] = "Ian Ilkeston";
    pic[3] = "Jenny Japan";
    pic[4] = "Keith Kimberley";
    pic[5] = "Lucy London";

    randomNum = Math.floor(Math.random()*pic.length);

    return pic[randomNum];
    }

    // -->



    <!--

    function tools() {
    pic = new Array();
    pic = new Array();
    pic[0] = "Pick a name from the hat";


    randomNum = Math.floor(Math.random()*pic.length);

    return pic[randomNum];
    }

    // -->



    </script>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    <body><center>
    <table width="80%" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td align="left" valign="top" bgcolor="#FFFFFFF"><table width="80%" height="220" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td height="60" colspan="2" align="center" valign="middle" bgcolor="#FFFFFF"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
    <td align="center" valign="middle"><font size="6" color="#000000"><strong>Big Wood School Random Name Generator</strong></font></td>
    </tr>
    </table></td>
    </tr>
    <tr>
    <td width="50%" rowspan="2" align="center" valign="middle" bgcolor="#FFFFFF"><p> <a href="#"
    onMouseOver="document.getElementById('myText1').innerHTML = pupilsname() ;" onMouseOut="document.getElementById('myText1').innerHTML = tools() ;""><font size="3"><font size=5><strong>Pick a pupil</strong></font></a>
    </li>
    </p></td>
    <td width="50%" bgcolor="#FFFFFF">&nbsp;</td>
    </tr>
    <tr>
    <td align="left" valign="top" bgcolor="#FFFFFF"><font size="6" color="#000000">
    <div id="myText1" onMouseOver="this.innerHTML = originaltext();">Pick a name from the hat</div>
    </font></td>
    </tr>
    </table></td>
    </tr>
    </table></center>
    </body>
    </html>


    However, what I want to do is have it so that it does NOT repeat a child if they have previously been selected.

    Is this possible and could someone please help me on this?


    Many thanks,

    Jay Dog

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    262
    Thanked 690 Times in 678 Posts

    Default

    There are two ways to do this, based on exactly what you're trying to do.

    1. Shuffle/randomize the list, then display the whole list (or part of it).
    PHP has a convenient function to randomize an array-- shuffle(). But that doesn't seem like a default in Javascript. However, you can find an equivalent here:
    https://www.google.com/search?q=java...+shuffle+array (The first result from stack overflow has an example of a shuffle() function for JS.)
    If you do that, then you can just display 1, then 2, then 3... etc.
    Alternatively, you could do this manually: pick a random individual and display it. Create a new array with all of the individuals but remove the one you just displayed. Repeat.
    It's probably easiest to randomize the whole array, but might depend on your exact purpose.
    Remember, you should probably create a copy of the array when you shuffle it-- unless you won't need the original order again. [It won't change the version saved on your server, of course, just the current data on the page for that user.]
    Finally, you could also randomize the array keys in a new array (so you get a new array like {4,1,2,0,5,3...}), then you can use those numbers to pick that element from the array. The advantage here is that you could continuously refer to the original array if there's a reason to do that.



    2. Pick a random individual; then pick another, but actually check to be sure that individual has not yet been chosen.
    I've used this method when there is no list, when I want to, for example, generate a random password that is not the same as another password already in the database. The logic goes like this:
    1) Generate a random password (a random string of characters).
    2) Check if that password exists in the database.
    --If yes, go to (1), continue from there.
    --If no, use that password.
    (You'd need to do this with a loop, and it can be a little tricky, but it works.)
    The only problem is the theoretical possibility that you'd run out of potential passwords, or that it would by chance continuously find used passwords and never make a new one. That's possible, just very very unlikely. I've used this method before and never had any real-life problems. If you're worried about it, you could have a limit and allow it to re-use a password after 1,000 tries or something.

    This method would actually work for what you're doing, but it may not be the easiest way. It's required if you don't have a list, but you do have a list (right? will you always have a list?).
    Basically this is the way to simulate what you're trying to do if you don't have a finite list to shuffle in the first place. If you do, there's probably no reason to use this version.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,694
    Thanks
    43
    Thanked 3,132 Times in 3,096 Posts
    Blog Entries
    12

    Default

    This would be simpler - move the array outside and before the function, return and remove one random item from it each time (Oh, and there were other problems, so copy all of the code in the below block, use it as a complete page):

    Code:
    <html>
     <head>
    <script type="text/javascript">
    
    var pic = [];
     pic[0] = "Adam Arnold";
     pic[1] = "Brenda Beeston";
     pic[2] = "Carl Carlton";
     pic[3] = "Deborah Daybrook";
     pic[4] = "Eric Eastwood";
     pic[5] = "Francis Findern";
     pic[6] = "Greg Grantham";
     pic[7] = "Hilda Heanor";
     pic[8] = "Ian Ilkeston";
     pic[9] = "Jenny Japan";
     pic[10] = "Keith Kimberley";
     pic[11] = "Lucy London";
    
    function pupilsname() {
    
     randomNum = Math.floor(Math.random()*pic.length);
     return pic.splice(randomNum, 1)[0] || "Sorry, no more names left.";
     }
    
     function tools() {
    
     var pic = new Array();
     pic[0] = "Pick a name from the hat";
    
    
     randomNum = Math.floor(Math.random()*pic.length);
    
     return pic[randomNum];
     }
    
    
     </script>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     </head>
     <body><center>
     <table width="80%" border="0" cellspacing="0" cellpadding="0">
     <tr>
     <td align="left" valign="top" bgcolor="#FFFFFFF"><table width="80%" height="220" border="0" cellspacing="0" cellpadding="0">
     <tr>
     <td height="60" colspan="2" align="center" valign="middle" bgcolor="#FFFFFF"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
     <tr>
     <td align="center" valign="middle"><font size="6" color="#000000"><strong>Big Wood School Random Name Generator</strong></font></td>
     </tr>
     </table></td>
     </tr>
     <tr>
     <td width="50%" rowspan="2" align="center" valign="middle" bgcolor="#FFFFFF"><p> <a href="#"
     onMouseOver="document.getElementById('myText1').innerHTML = pupilsname() ;" onMouseOut="document.getElementById('myText1').innerHTML = tools() ;""><font size="3"><font size=5><strong>Pick a pupil</strong></font></a>
     </li>
     </p></td>
     <td width="50%" bgcolor="#FFFFFF">&nbsp;</td>
     </tr>
     <tr>
     <td align="left" valign="top" bgcolor="#FFFFFF"><font size="6" color="#000000">
     <div id="myText1" onMouseOver="this.innerHTML = tools();">Pick a name from the hat</div>
     </font></td>
     </tr>
     </table></td>
     </tr>
     </table></center>
     </body>
     </html>
    Last edited by jscheuer1; 03-27-2013 at 08:15 PM. Reason: fixed other problems
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Random Background Image - No repeat?
    By Silent_Bob in forum JavaScript
    Replies: 13
    Last Post: 02-07-2012, 06:49 PM
  2. background repeat only 20%?
    By rekk in forum CSS
    Replies: 5
    Last Post: 12-15-2009, 01:16 PM
  3. Replies: 4
    Last Post: 11-22-2008, 07:42 PM
  4. random image without repeat
    By dennismonsewicz in forum JavaScript
    Replies: 1
    Last Post: 07-04-2008, 06:35 AM
  5. Random Images but no repeat?
    By the_alpine in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 01-23-2005, 07:54 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •