+ Reply to Thread
Results 1 to 5 of 5

Thread: random number generator

  1. #1
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,022
    Blog Entries
    1

    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 05:57 PM.
    "Whenever you remember, I'll be there/
    Remember how we reached that dream together" - Carrie Underwood

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    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.

  3. #3
    Programming Expert Sionofdarkness is an unknown quantity at this point
    Join Date
    Jul 2006
    Posts
    383

    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.

  4. #4
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,022
    Blog Entries
    1

    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.
    "Whenever you remember, I'll be there/
    Remember how we reached that dream together" - Carrie Underwood

  5. #5
    Programmer CygnetGames is an unknown quantity at this point CygnetGames's Avatar
    Join Date
    May 2007
    Location
    York, England
    Posts
    118

    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.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. Guess a Number
    By 3n! in forum C and C++
    Replies: 2
    Last Post: 12-02-2007, 03:44 PM
  2. Number Generator
    By Beginning-now in forum General Programming
    Replies: 3
    Last Post: 11-03-2007, 06:36 AM
  3. Floating Point random number generator
    By Paradine in forum PHP Tutorials
    Replies: 0
    Last Post: 08-26-2007, 02:09 PM
  4. How do I program a random generator?
    By eddiewillers in forum General Programming
    Replies: 4
    Last Post: 07-24-2007, 02:26 PM
  5. I need to generate a random number in PHP
    By dirkfirst in forum PHP Forum
    Replies: 7
    Last Post: 07-05-2006, 09:58 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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