Lost Password?

  #1 (permalink)  
Old 03-03-2007, 10:31 AM
reachpradeep's Avatar   
reachpradeep reachpradeep is offline
Learning Programmer
 
Join Date: Mar 2007
Location: India
Posts: 41
Rep Power: 5
reachpradeep is on a distinguished road
Send a message via MSN to reachpradeep
Default Javascript: Prime or not

This script is a slightly modified version of "Unique Random Numbers". In this script it becomes easier to implement more than one instances of "Picking Unique Random Numbers".

This JavaScript picks up a number of unique random elements from an array.
For example; if you have an array myArray consisting of 10 elements and want to pick 5 unique random elements. Suppose initially myArray[3] is picked randomly, then myArray[3] should not be picked again.

Code:
<html>
<head>
<title>Unique Random Numbers II</title>
<!--BEGIN HEAD SECTION CODE-->
<script language="JavaScript">
// Unique Random Numbers II
// -Picks a number of unique random numbers from an array
// By Premshree Pillai
// http://www.qiksearch.com, http://javascript.qik.cjb.net

function pickNums(nums, numArr, pickArr, count, doFlag, iterations)
{
 iterations+=1;
 var currNum = Math.round((numArr.length-1)*Math.random());
 if(count!=0)
 {
  for(var i=0; i<pickArr.length; i++)
  {
   if(numArr[currNum]==pickArr[i])
   {
    doFlag=true;
    break;
   }
  }
 }
 if(!doFlag)
 {
  pickArr[count]=numArr[currNum];
  document.write('<b>' + numArr[currNum] + '</b> <font color="#808080">|</font> ');
  /* Modify above line for a different format output */
  count+=1;
 }
 if(iterations<(numArr.length*3)) // Compare for max iterations you want
 {
  if((count<nums))
  {
   pickNums(nums, numArr, pickArr, count, doFlag, iterations);
  }
 }
 else
 {
  location.reload();
 }
}
</script>
</head>
<!--END HEAD SECTION CODE-->
<body bgcolor="#FFFFFF">

<!--BEGIN BODY SECTION CODE-->
<script language="JavaScript">
var numArr1 = new Array("0","1","2","3","4","5","6","7","8","9"); // Add elements here
var pickArr1 = new Array(); // The array that will be formed
var count1=0;
var doFlag1=false;
var iterations1=0;

pickNums(5, numArr1, pickArr1, count1, doFlag1, iterations1); // Call the function, the argument is the number of elements you want to pick.
             // Here we pick 5 unique random numbers
</script>
<!--END BODY SECTION CODE-->

</body>
</html>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 08-21-2007, 12:14 PM
RikkoSuperb's Avatar   
RikkoSuperb RikkoSuperb is offline
Newbie
 
Join Date: Aug 2007
Location: holywood, county down, northern ireland
Age: 26
Posts: 14
Rep Power: 0
RikkoSuperb is on a distinguished road
Default

Very useful script reachpradeep, Im looking at the code to try to understand what is happening and why. Is it possible to alter the length of the array and its contents??
__________________
RikkoSuperb
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-21-2007, 10:37 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,346
Last Blog:
PHP Function Overloadi...
Rep Power: 50
John is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of light
Send a message via AIM to John
Default

Nice code, but as your title suggests, what does this have to do with 'prime' numbers? ie) 3, 5, 7, 11, 13
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-22-2007, 08:59 AM
RikkoSuperb's Avatar   
RikkoSuperb RikkoSuperb is offline
Newbie
 
Join Date: Aug 2007
Location: holywood, county down, northern ireland
Age: 26
Posts: 14
Rep Power: 0
RikkoSuperb is on a distinguished road
Default

True enough Sidewinder, when you run the code you never seem to get 5 prime numbers
__________________
RikkoSuperb
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-23-2007, 08:58 AM
RikkoSuperb's Avatar   
RikkoSuperb RikkoSuperb is offline
Newbie
 
Join Date: Aug 2007
Location: holywood, county down, northern ireland
Age: 26
Posts: 14
Rep Power: 0
RikkoSuperb is on a distinguished road
Default

ah, I get you now.
// I suppose life does become clearer once you read the added comments...!
__________________
RikkoSuperb
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Counting Files with JavaScript Matt JavaScript and CSS 5 08-01-2007 12:02 PM
URGENT a small javascript project $35! Hyde Request Services (Paid) 1 06-12-2007 07:18 AM
Javascript accessing PHP Ronin JavaScript and CSS 0 11-08-2006 06:29 PM
Javascript Problem on Firefox streulich JavaScript and CSS 10 10-17-2006 06:40 PM
JSP = JavaScript? Ronin JavaScript and CSS 1 08-13-2006 06:56 AM


All times are GMT -5. The time now is 10:51 PM.

Contest Stats

John ........ 87.50000
dargueta ........ 75.00000
Xav ........ 50.00000
MeTh0Dz ........ 20.00000
gaylo565 ........ 18.00000
Johnnyboy ........ 3.00000

Contest Rules

Ads