I'm making a random number generator to help with a tournament.
Here's my code:
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.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>
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.
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.
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.
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.
You could do it by shuffling the list of of numbers.
i.e.
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.Code:list1 = array(1,2,3,4,5,6,7,8); list2 = shuffle(list1);
The only problem with this is that Javascript doesn't have a built-in shuffle function, but this one looks good.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks