Digital Workshop

Welcome to the Digital Workshop Message Boards
It is currently December 22nd, 2024, 3:56 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: 12 random numbers, 1-36, no repeats
PostPosted: January 28th, 2014, 2:52 am 
Offline
Godlike
Godlike

Joined: November 11th, 2004, 1:18 pm
Posts: 1213
Location: New York
Opus: Opus Pro 9.75
OS: Windows 10 Pro 64 bit
System: Core i7, 16G RAM, Nvidia 640GT (desktop), plus Windows 10 and Android tablets
Hi,

Was looking for a random number no repeat script.

Checking this forum and elsewhere online, I came up with a script that works, but is very long (my scripting skills weren't terribly efficient).

I suspect there is a more efficient way to populate the arrays and set up checking loops.

Rather than put the entire lengthy script below, I can include a smaller version.

I would appreciate any suggestions on how to write this more efficiently (and shorter).

Code:
function chooser100(){
var myArray = new Array();
myArray[0] ="F1"; 
myArray[1] ="F2";
myArray[2] ="F3";
myArray[3] ="F4";
myArray[4] ="F5";
myArray[5] ="F6";
myArray[6] ="F7";
myArray[7] ="F8";
myArray[8] ="F9";
myArray[9] ="F10";
myArray[10] ="F11";
myArray[11] ="F12";
myArray[12] ="F13";
myArray[13] ="F14";
myArray[14] ="F15";
myArray[15] ="F16";
myArray[16] ="F17"; 
myArray[17] ="F18";
myArray[18] ="F19";
myArray[19] ="F20";
myArray[20] ="F21";
myArray[21] ="F22";
myArray[22] ="F23";
myArray[23] ="F24";
myArray[24] ="F25";
myArray[25] ="F26";
myArray[26] ="F27";
myArray[27] ="F28";
myArray[28] ="F29";
myArray[29] ="F30";
myArray[30] ="F31";
myArray[31] ="F32";
myArray[32] ="F33";
myArray[33] ="F34";
myArray[34] ="F35";
myArray[35] ="F36";

var len = myArray.length;
var choice = new Array();

for (var i = 0; i<=11; i++) {
var rand1 = Math.floor(Math.random() * len); 
choice[i] = myArray[rand1];

for (k=0; k<=i-1; k++) {
if (choice[k] == choice[i]) {
i--;

}
}
}
if (choice[0] == "F1")
{
F1.Rotate(90)
}
if (choice[0] == "F2")
{
F2.Rotate(90)
}
if (choice[0] == "F3")
{
F3.Rotate(90)
}
if (choice[0] == "F4")
{
F4.Rotate(90)
}
if (choice[0] == "F5")
{
F5.Rotate(90)
}
if (choice[0] == "F6")
{
F6.Rotate(90)
}
if (choice[0] == "F7")
{
F7.Rotate(90)
}
if (choice[0] == "F8")
{
F8.Rotate(90)
}
if (choice[0] == "F9")
{
F9.Rotate(90)
}
if (choice[0] == "F10")
{
F10.Rotate(90)
}

//etc.-up to F36, then the same for array choices [1], [2], [3], [4] and [5], then on to more for Rotate.(270)-much, much too long, but works.


One item that I tried, without success:

Code:
for (var m = 0;m<=5;m++){
if ((choice[m] == "F1")||(choice[m] == "F2")||(choice[m] == "F3")||(choice[m] == "F4")||(choice[m] == "F5")||(choice[m] == "F6")||(choice[m] == "F7")||
(choice[m] == "F8")||(choice[m] == "F9")||(choice[m] == "F10")||(choice[m] == "F11")||(choice[m] == "F12")||(choice[m] == "F13")||(choice[m] == "F14")||
(choice[m] == "F15")||(choice[m] == "F16")||(choice[m] == "F17")||(choice[m] == "F18")||(choice[m] == "F19")||(choice[m] == "F20")||(choice[m] == "F21")||
(choice[m] == "F22")||(choice[m] == "F23")||(choice[m] == "F24")||(choice[m] == "F25")||(choice[m] == "F26")||(choice[m] == "F27")||(choice[m] == "F28")||
(choice[m] == "F29")||(choice[m] == "F30")||(choice[m] == "F31")||(choice[m] == "F32")||(choice[m] == "F33")||(choice[m] == "F34")||(choice[m] == "F35")||
(choice[m] == "F36"))
{
choice[m].Rotate(90)
}
}


Must be a simpler way to write this? :oops:

_________________
Stephen


For this message Stephen has been thanked by : mackavi


Top
 Profile  
 
 Post subject: Re: 12 random numbers, 1-36, no repeats
PostPosted: February 3rd, 2014, 9:19 am 
Offline
Godlike
Godlike
User avatar

Joined: March 21st, 2007, 10:44 am
Posts: 3188
Location: UK
Opus: Evolution
You don't specify what type of publication you're building :-(

The following works for Opus Pro Types:

Code:
/*USAGE
len = number range - IE 36 is 1,2,3...34,35,36
sub = number sub-range - IE 4 is first four random numbers
*/

doRandomRotate(10,4) //generate numbers 1 to 10 and pick the first four random numbers

function doRandomRotate(len, sub)
{
   var a  =new Array;

   for (var i=0;i<len;i++) //generate numbers
      a[i] = i+1

      while ( --len ) //shuffle numbers
      {
        var j = Math.floor( Math.random() * ( len + 1 ) )
        var tempi = a[len]
        var tempj = a[j]
        a[len] = tempj
        a[j] = tempi
      }

   for (var i=0;i<sub;i++) //pick 'sub' and perform action
      eval("F"+a[i] +".Rotate(90)")
}


For example: If I have 35 frames on screen named F1 to F35 and want to rotate a random selection of 11 frames, I call

Code:
doRandomRotate(35,11);


Mack
https://twitter.com/imsmackavi

_________________
When you have explored all avenues of possibilities, what ever remains, how ever improbable, must be the answer.

Interactive Solutions for Business & Education
Learn Anywhere. Learn Anytime.

www.interaktiv.co.uk
+44 (0) 1395 548057


Top
 Profile Visit website  
 
 Post subject: Re: 12 random numbers, 1-36, no repeats
PostPosted: February 3rd, 2014, 1:12 pm 
Offline
Godlike
Godlike

Joined: November 11th, 2004, 1:18 pm
Posts: 1213
Location: New York
Opus: Opus Pro 9.75
OS: Windows 10 Pro 64 bit
System: Core i7, 16G RAM, Nvidia 640GT (desktop), plus Windows 10 and Android tablets
Hi Mack,

Much thanks!

I'll try it out (36 puzzle pieces). (Apologies: is a "standard" Opus Pro 9 pub for Windows.)

_________________
Stephen


For this message Stephen has been thanked by : mackavi


Top
 Profile  
 
 Post subject: Re: 12 random numbers, 1-36, no repeats
PostPosted: February 3rd, 2014, 4:55 pm 
Offline
Godlike
Godlike
User avatar

Joined: March 21st, 2007, 10:44 am
Posts: 3188
Location: UK
Opus: Evolution
That should work fine then.

Just create your frames as F1 to F36 and run the function as described to randomly rotate X pieces of the set.

Mack

_________________
When you have explored all avenues of possibilities, what ever remains, how ever improbable, must be the answer.

Interactive Solutions for Business & Education
Learn Anywhere. Learn Anytime.

www.interaktiv.co.uk
+44 (0) 1395 548057


Top
 Profile Visit website  
 
 Post subject: Re: 12 random numbers, 1-36, no repeats
PostPosted: February 3rd, 2014, 5:26 pm 
Offline
Godlike
Godlike

Joined: November 11th, 2004, 1:18 pm
Posts: 1213
Location: New York
Opus: Opus Pro 9.75
OS: Windows 10 Pro 64 bit
System: Core i7, 16G RAM, Nvidia 640GT (desktop), plus Windows 10 and Android tablets
Hi Mack,

Yes, this works and reduces greatly the size of the script. Very nice!

A little more complexity (and puzzle challenge):

What if I would like to rotate 6 @ 90° and 6 more @ 270 :total of 12 from 36, comprised of 6 (rotate 90°) and 6 (rotate 270°).

I tried to substitute the following, some success, but not randomly chosen (instead uses F1-18 or F19-36. Must be a better, random method?):

Code:
for (var i=0;i<sub;i++){     
   if (a[i]<=18){
   eval("F"+a[i] +".Rotate(90)")
   }
   if (a[i]>18){
   eval("F"+a[i] +".Rotate(270)")
   }
   }


So, not quite sure how to change the script for this, especially without repeats?

_________________
Stephen


For this message Stephen has been thanked by : mackavi


Top
 Profile  
 
 Post subject: Re: 12 random numbers, 1-36, no repeats
PostPosted: February 3rd, 2014, 9:47 pm 
Offline
Godlike
Godlike
User avatar

Joined: March 21st, 2007, 10:44 am
Posts: 3188
Location: UK
Opus: Evolution
That depends on whether set two has to be distinct - IE it cannot have already occurred in set one.

If this is the case, you simply need to repeat the whole loop, adjusting for the next range in the array:

Code:
   for (var i=0;i<sub;i++) //pick 'sub' and perform action
      eval("F"+a[i] +".Rotate(270)")


Change the var i=0 and i<sub sections to accommodate the new range with with fixed values or by passing a new parameter to the function. EG:

Code:
   for (var i=10;i<20;i++) //pick 'sub' and perform action
      eval("F"+a[i] +".Rotate(180)")


Will select those elements from the original shuffle

Alternatively, if it doesn't matter whether the sets are distinct - IE a piece can be in the 90 and 270 set, then I'd just convert the Rotate value to a variable, pass it as a parameter and call the function each time specifying the angle.

Mack
https://twitter.com/imsmackavi

_________________
When you have explored all avenues of possibilities, what ever remains, how ever improbable, must be the answer.

Interactive Solutions for Business & Education
Learn Anywhere. Learn Anytime.

www.interaktiv.co.uk
+44 (0) 1395 548057


For this message mackavi has been thanked by : Stephen


Top
 Profile Visit website  
 
 Post subject: Re: 12 random numbers, 1-36, no repeats
PostPosted: February 3rd, 2014, 10:12 pm 
Offline
Godlike
Godlike

Joined: November 11th, 2004, 1:18 pm
Posts: 1213
Location: New York
Opus: Opus Pro 9.75
OS: Windows 10 Pro 64 bit
System: Core i7, 16G RAM, Nvidia 640GT (desktop), plus Windows 10 and Android tablets
Thank you again, Mack.

Used the "distinct" sets, and it works great!

_________________
Stephen


For this message Stephen has been thanked by : mackavi


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group