Exforsys

Online Training

Prime or not

This is a discussion on Prime or not within the Java forums, part of the Programming Talk category; This script is a slightly modified version of "Unique Random Numbers". In this script it becomes easier to ...


Go Back   Exforsys > Programming Talk > Java

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-11-2006, 01:10 PM
Member
 
Join Date: Apr 2006
Posts: 30
insane is on a distinguished road
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:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<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
  #2 (permalink)  
Old 05-11-2006, 10:16 PM
Member
 
Join Date: Apr 2006
Posts: 85
cbdarts is an unknown quantity at this point
Quote:
Originally Posted by insane
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:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<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>

VERY interesting, thank you... Except it may have been nicer to make it a blog...

Chaitanya
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-01-2006, 05:27 AM
Senior Member
 
Join Date: May 2006
Posts: 99
klorpet is on a distinguished road
Cool, nice post insane.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools

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
Prime or not insane Java 0 05-11-2006 01:09 PM
prospective PM of India India Chit-Chat 2 05-22-2004 10:45 AM


All times are GMT -4. The time now is 12:25 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.