View Full Version : math.random with exclusion
blueflowers
03-07-2007, 07:33 PM
Okay I've scoured Google and this forum but haven't found what I need or anything similiar (even though this shouldn't be too tough).
What I have is a script that displays 3 of 5 div areas on a web page.
The problem is that whenever math.random returns the same number it obviously won't keep to my 'showing 3 items at all times' model and just displays 2 or 1 item(s).
The script:
function randomize(){
var rand = 0;
var num = 0;
rand = Math.round(Math.random() * 5);
num = rand;
document.getElementById('c'+rand).style.display='';
}
I also have a script that loops 3 times through the random method:
function startRand(){
var many = 3;
for(var i=0;i<many;i++){
randomize();
}
}
Thanks in advance!
pcbrainbuster
03-07-2007, 07:40 PM
I suggest you use if - else and if else in your script... (i am thinking you know how to do that)
blueflowers
03-07-2007, 07:48 PM
of course lol
I am a php/asp programmer (not so hot with the JS ;) ) and what I can't figure out with the javascript is how to set the variable to be remembered to exclude the number...
so in short, (pseudo)
num = math.random;
rand = num;
while(num != rand){
document.getElementById('c'+rand).style.display='';
}
or some variation of this, but I just cannot get it working, it won't 'remember' the var num (hence my question here :) ).
Cheers
jscheuer1
03-07-2007, 11:18 PM
This litte bit:
var many = 3;
for(var i=0;i<many;i++){
randomize();
}
will run randomize() 4 times (0 through 3, the value of many). It will run once each loop of the for statement producing a different random number each time. If you want to use only one random number for all the loops, set it outside of the for statement.
Bob90
03-09-2007, 10:38 PM
I know its long, but its comprehensive.
//Robs Fantastic Random Number Generator
RNA = new Array();//Random Number Array, Stores Random Numbers for accessing later
var lowest_number = 1; //lowest number wanted
var highest_number = 10; //highest number wanted
var unique_numbers = true; //if each number is unique, true=all numbers different, fasle=numbers may be the same
var amount_of_numbers = 10; //amount of numbers you want
//fill next free array slot with a random number
function addRandomNumber()
{
foundUniqueNumber = false;
range = (highest_number-lowest_number)+1;
if(!unique_numbers)
{
RNA[RNA.length] = Math.floor(Math.random()*range) + lowest_number
}
else
{
while(foundUniqueNumber == false)
{
var y = Math.floor(Math.random()*range) + lowest_number;
foundUniqueNumber = randomNumberUnique(y)
if(foundUniqueNumber)
{
RNA[RNA.length] = y;
}
}
}
}
//Checks if random number already exists in array.
function randomNumberUnique(r_num)
{
toReturn = true;
for(var x=0; x<RNA.length; x++)
{
if(RNA[x] == r_num)
{
toReturn = false;
break;
}
}
return toReturn;
}
//check if amount of unique numbers required is less than or equal to amount possible
if(amount_of_numbers <= (highest_number-lowest_number)+1)
{
for(var z=0; z<amount_of_numbers; z++)
{
addRandomNumber()
}
}
else
{
alert("You have chosen to have unique numbers, but don't have a large enough range. Try reducing the amount of numbers required or increasing the range.")
}
alert(RNA)
All you need to do is change the top few variables and then the RNA array will be populated with your numbers. You can access them like a normal array.
Bob90
03-09-2007, 11:10 PM
The code doesn't work if unique numbers are not used because of the if statement at the bottom. I'll fix it tommorrow and repost.
jscheuer1
03-10-2007, 04:30 AM
If all you want are 3 unique random numbers from 0 through 5:
<script type="text/javascript">
var rand_3=[];
Array.prototype.removeDuplicates = function () {
for(var i = 0; i < this.length; i++){
for (var i_tem = 0; i_tem < this.length; i_tem++)
if(this[i] == this[i_tem]&&i!==i_tem){
this.splice(i_tem,1);
}
}
}
while(rand_3.length<3){
rand_3[rand_3.length]=Math.round(Math.random() * 5);
rand_3.removeDuplicates();
}
alert(rand_3.join(','));
</script>
jscheuer1
03-10-2007, 07:29 AM
Here's an even simpler method (thanks to mwinter's random sort function):
<script type="text/javascript">
var nums=[0,1,2,3,4,5].sort(function() {return 0.5 - Math.random();})
alert(nums[0]+','+nums[1]+','+nums[2])
</script>
blueflowers
03-14-2007, 02:38 PM
@jscheuer1
The second script didn't seem to pull unique numbers for some reason but your first function worked like a charm :)
Thanks a lot!
Finished code (in case anyone ever needs it)
function randomize(){
var rand_3=[];
var many = 3;
Array.prototype.removeDuplicates = function () {
for(var i = 0; i < this.length; i++){
for (var i_tem = 0; i_tem < this.length; i_tem++)
if(this[i] == this[i_tem]&&i!==i_tem){
this.splice(i_tem,1);
}
}
}
while(rand_3.length<many){
rand_3[rand_3.length]=Math.round(Math.random() * 5);
rand_3.removeDuplicates();
}
for(var x = 0; x < rand_3.length; x++){
document.getElementById('c'+rand_3[x]).style.display='';
}
}
jscheuer1
03-14-2007, 05:20 PM
The simpler (shorter actually) script will output numbers randomly. I think you just misunderstand how to use them to display your content.
Where you have:
for(var x = 0; x < rand_3.length; x++){
document.getElementById('c'+rand_3[x]).style.display='';
}
Using the code in my second example would do it like so:
for(var x = 0; x < 3; x++){
document.getElementById('c'+nums[x]).style.display='';
}
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.