Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generating Unique Random Numbers

Status
Not open for further replies.

jonthequik

Programmer
Aug 6, 2000
40
US
I am wanting to set up a quiz that pulls 35 questions randomly from a list of 100 questions. Basically, I need to randomly generat 35 numbers from 0 - 100 and insure that none of them are duplicated.

Here's what I have so far:
function getRanNum() {
a = new Array(35);
for (i = 0; i < 35; i++) {
a = Math.round(Math.random()*100);
}
}
</SCRIPT>

Now, I need to runs some series of loops or something to cross-reference each number with the rest in the array and change it if it's already used...or something that works like that.

Jonathan
 
this method is from my JSX library:

function Array_contains(zy)
{var xi = this.length;for(var i=0;i<xi;i++){if(this==zy){return true}}return false}
Array.prototype.contains=Array_contains;

it will allow you to call myarray.contains(aval)... the method returns true if the value is in the array or false if not... so modify your code to look like this:

function getRanNum() {
a = new Array(35);
for (i = 0; i < 35; i++) {
randnum = Math.round(Math.random()*100
if(a.contains(randnum){i--;continue;}
else{a = randnum;}
}
}

and include my code above it... jared@aauser.com
 
Dear jared,

I'm afraid your 'contains' function does not work properly...

> if(this==zy){

will not produce the desired behavior. Even if you fix it...

if(this[index]==zy)

it only works for numbers pretty much. A more complete solution is to do something like a 'find' function, it might look like this:

function array_find( input){

var nret = -1; // not found
for(n=0; n<this.length && nret < 0; n++){

// if both are objects that have a function named
// 'equals' then use that to determine equality
if ( 'object' == typeof(this[n])
&& 'object' == typeof(input)
&& 'function' == typeof(this[n].equals)
&& 'function' == typeof(input.equals) ){

if ( this[n].equals(input))
nret = n;
}
else{
if ( this[n] == input)
nret = n;
}
}

return nret;
}
Array.prototype.find = array_find;


Then an example of how a String.equals function could look:

function string_equals(sin){

if ( this.indexOf(sin) == 0 && this.length == sin.length)
return true;

return false;
}
String.prototype.equals = string_equals;

You can see this work by running a function like this:

function testContains(n){
if ('undefined' == n) n = 4;
var a = new Array(1,4,&quot;hello world&quot;,5,7);
if ( a.contains(n))
alert(&quot;a has &quot; + n);
else
alert(&quot;No &quot; + n + &quot; found&quot;);

if ( -1 < a.find(n))
alert( n + &quot; is element &quot; + a.find(n));

if ( -1 < a.find(&quot;hello world&quot;))
alert( &quot;hello world found at element &quot; + a.find(&quot;hello world&quot;));
}

Hope this helps
-pete
 
function Array_contains(zy)
{var xi = this.length;for(var i=0;i<xi;i++){if(this==zy){return true}}return false}
Array.prototype.contains=Array_contains;

x=new Date
d=[&quot;a&quot;,1,&quot;7&quot;,9,x]

it was supposed to look like this...I must have grabbed a non-working copy... the following returns true for array d:

d.contains(x)
d.contains('a')
d.contains(7)

number,string,object types seem to be accounted for...

jared@aauser.com
 
very true...it compares objects by reference, not by value...quick question pete, do you know if IE supporters something like a server push... need to integrate a chat client with our dialing web app here and management decided to use server push-type technology as opposed to java (probably cuz I know it best of anyone here and I cant do a darn thing in java yet :)

jared@aauser.com
 
Dear jaredn,

I'm not really following your explanation. IE has support for what Microsoft calls 'channels'. You might want to research that on msdn.microsoft.com

That may be way off the mark of what you are asking about.

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top