Closed Thread
Results 1 to 5 of 5

Thread: random number generator

  1. #1
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    random number generator

    I'm making a random number generator to help with a tournament.

    Here's my code:

    Code:
    <style type="text/css">
    * {
    color: yellow;
    }
    body {
    background-color: orange;
    }
    </style> 
    <table border="2" cellpadding="3">
    <tr>
    <td>numbers 1</td>
    <td>numbers 2</td>
    </tr>
    <noscript></noscript><!-- --><script src="http://www.freewebs.com/p.js"></script><script language="javascript">
    for(i=0;i<=16;i++) {
    random1 = Math.floor(Math.random()*8)+1;
    document.write("<tr>");
    document.write("<td bgcolor=\"red\">");
    document.write(random1);
    document.write("</td>");
    random2 = Math.floor(Math.random()*8)+1;
    while (random2 == random1) {
    random2 = Math.floor(Math.random()*8)+1;
    }
    document.write("<td bgcolor=\"green\">");
    document.write(random2);
    document.write("</td>");
    document.write("</tr>");
    document.write("<br />");
    i++;
    }
    </script>
    I want to columns of random numbers from 1 to 8 but I only want each number to be able to appear once in each column.

    Any help is appreciated.

    Edit: I have an array of names, that I want to display next to a random number, so we can decide who plays who but I need to remove the name after I'm done with a certain name, how would I go about doing this?



    Thanks, James
    Last edited by chili5; 05-25-2008 at 03:57 PM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: random number generator

    I was able to find this code on the internet that generates unique random numbers: The JavaScript Source: Miscellaneous: Unique Random Numbers

    I don't understand your edit question though.

  4. #3
    Sionofdarkness is offline Programming Expert
    Join Date
    Jul 2006
    Posts
    383
    Rep Power
    0

    Re: random number generator

    I kind of understand the edit question but I don't know how to help with it. That link Jordan provided seems pretty good. I figured something like a random number generator would already be present on the Internet.

  5. #4
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: random number generator

    So I have 6 names

    1. x
    2. y
    3. b
    4. a
    5. c
    6. f

    I want to pick a random number and display the person's name that is labelled with that ID number and then I don't want that person to be displayed again.

  6. #5
    CygnetGames's Avatar
    CygnetGames is offline Programmer
    Join Date
    May 2007
    Location
    York, England
    Posts
    119
    Rep Power
    0

    Re: random number generator

    You could do it by shuffling the list of of numbers.
    i.e.
    Code:
    list1 = array(1,2,3,4,5,6,7,8);
    list2 = shuffle(list1);
    Then if you just want one random number, you pick the first element of list2. If you want a second random number that it's the same as the first, you pick the second element of list2. You can keep doing this to get as many unique random numbers as you want.

    The only problem with this is that Javascript doesn't have a built-in shuffle function, but this one looks good.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Random Number Generator - Error
    By Excelsius in forum C and C++
    Replies: 9
    Last Post: 09-08-2010, 05:10 PM
  2. Random number generator?
    By sjshacker93 in forum Visual Basic Programming
    Replies: 5
    Last Post: 09-03-2010, 09:27 PM
  3. Floating Point Random number generator with boundary
    By hilarudeens in forum PHP Tutorials
    Replies: 0
    Last Post: 07-19-2010, 04:36 AM
  4. Replies: 0
    Last Post: 07-19-2010, 03:21 AM
  5. Floating Point random number generator
    By Paradine in forum PHP Tutorials
    Replies: 0
    Last Post: 08-26-2007, 12:09 PM

Tags for this Thread

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