Digital Workshop

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

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Generate a list of random numbers between X and Y.
PostPosted: May 18th, 2010, 2:29 pm 
Offline

Joined: November 25th, 2004, 1:24 pm
Posts: 512
Location: Scotland
Opus: 9.75
OS: Win 10
System: Asus i7-7700K 16Gb
Here is a function for creating a list of random numbers between 'myStart' and 'MyStop'. The output is an array 'MyRandoms[MyCount]' , where MyCount is the number of random numbers. The array starts at [1] not [0].

The script has not been error trapped. It will work where MyStop is greater than MyStart. MyStart can be zero, but negative numbers will crash it.

I'va also attached an imp which uses it.



Code:
function MyRandom(MyStart,MyStop)
//The output from this function is an array MyRandoms[MyCount], where MyCount
//is number of random numbers. The array starts at [1] not [0]
{
   
         MyList = new Array()
         n=parseInt(MyStart)   
         MyStop=parseInt(MyStop)
     while (n <= MyStop)   
         
           {
            MyList[n]=n
         n++   
           }
   
   
               // finish creating an array between from MyStart to MyStop   
               //------------------------------------------------------------------------------------   
              // this bit takes the list of numbers produced above and puts them into an array starting at 0
   
        count=0
        m=0
        MyShort=new Array()
        l=0//loop counter


            for(l=0; l<= (MyList.length-1);l++)
               {
                 if(MyList[l] != null)
                  {
                   count=count+1
                   MyShort[m]=MyList[l]
                   m++                   
                  }

               }
//-------------------------------------------------------------------------------
// This bit randomises the array of numbers(MyShort[] and puts them into a new arrayMyRandoms[]
               
        MyRandoms=new Array()
        MyCount = 1
        k=m           // k is the loop control
        while (MyCount <= k)
           {
                MyLoop = true
                while (MyLoop == true)
                    {
                     myRandom = String.random(k)                  
                         if (MyShort[myRandom] != null)
                           {
                            MyLoop = false                              
                           }
                   }

                
         MyRandoms[MyCount] = MyShort[myRandom]
         MyShort[myRandom] = null
                  
            //Display list
            Debug.trace(MyRandoms[MyCount]+"   "+'\n')         
           MyCount++      
             
}
Debug.trace('\n'+'\n')

}//EOFunction




Sandy


You do not have the required permissions to view the files attached to this post.

_________________
Whoever designed this, never actually used it!


For this message sandyn has been thanked by : mackavi


Top
 Profile  
 
 Post subject: Great Tipe!
PostPosted: June 11th, 2010, 4:16 pm 
Offline

Joined: December 13th, 2008, 3:15 pm
Posts: 152
Location: Brasil
Opus: 7
OS: Windows 7 x64
System: Asus P5Q3 Deluxe, Core 2 Duo E8500, 8 Gb DDR3 1333 mhz, His IceQ5 5770 HD Graphic Card, Samsung T220 Monitor; Lenovo G550 T6600, 4 GB DDR3 - 15,6".
Great Sandy.

This tip may apply in the use of random chapters as well, correct?

It's just one of the practical uses, but probably, other users of this forum will adapt this fabulous script to your own needs!

Great Post!

_________________
"To the ignorant, old age is the winter of life, for the wise, is the season of harvest."
It is not easy to translate the world into a binary code of 0s and 1s, but in scripts, anything is possible!


For this message NilsonBrasil has been thanked by : mackavi


Top
 Profile  
 
 Post subject: Re: Generate a list of random numbers between X and Y.
PostPosted: June 11th, 2010, 9:53 pm 
Offline

Joined: November 25th, 2004, 1:24 pm
Posts: 512
Location: Scotland
Opus: 9.75
OS: Win 10
System: Asus i7-7700K 16Gb
yes, it can be used for random chapters. I created a function, so it can be used in any application.

Sandy

_________________
Whoever designed this, never actually used it!


For this message sandyn has been thanked by : mackavi, NilsonBrasil


Top
 Profile  
 
 Post subject: Re: Generate a list of random numbers between X and Y.
PostPosted: June 12th, 2010, 5:20 am 
Offline
Godlike
Godlike

Joined: November 12th, 2005, 1:56 am
Posts: 1474
Location: SFBay Area
Opus: OpusPro v9.0x, & Evol.
OS: Vista32
System: Core 2 duo 2Ghz, RAM 3GB, Nvidia Go 7700 - laptop
Nilson,

This would be a good opportunity to 'give thanks' to Sandyn FORMALLY via the give-thanks or give-gratitude feature of the Bulletin Board.

Hey... :oops: I've looked and I cannot figure out HOW to do that.

_________________
_good things come to those who wait(0)_


For this message Lar_123 has been thanked by : mackavi


Top
 Profile  
 
 Post subject: Generate a list of random numbers between X and Y.
PostPosted: June 12th, 2010, 6:21 am 
Offline

Joined: December 13th, 2008, 3:15 pm
Posts: 152
Location: Brasil
Opus: 7
OS: Windows 7 x64
System: Asus P5Q3 Deluxe, Core 2 Duo E8500, 8 Gb DDR3 1333 mhz, His IceQ5 5770 HD Graphic Card, Samsung T220 Monitor; Lenovo G550 T6600, 4 GB DDR3 - 15,6".
Why do you don't give thanks?

It's so easy! Even more to me, I live all the time asking for help!

And just click in the thanks button below the post thanks!

Thanks, Opus Lovers... Very Thankfull!! :lol:

_________________
"To the ignorant, old age is the winter of life, for the wise, is the season of harvest."
It is not easy to translate the world into a binary code of 0s and 1s, but in scripts, anything is possible!


For this message NilsonBrasil has been thanked by : mackavi


Top
 Profile  
 
 Post subject: Re: Generate a list of random numbers between X and Y.
PostPosted: April 1st, 2011, 6:18 am 
Offline

Joined: February 24th, 2011, 1:08 pm
Posts: 3
Opus: 7
First of all i will thank Sandy for this script.
I have one question, the result is now displayed on a debug screen.
how can i store the result in a variable so that i can display the result one after each other.

Regards,

Ad


For this message internetsoftware has been thanked by : mackavi


Top
 Profile  
 
 Post subject: Re: Generate a list of random numbers between X and Y.
PostPosted: April 1st, 2011, 1:12 pm 
Offline

Joined: November 25th, 2004, 1:24 pm
Posts: 512
Location: Scotland
Opus: 9.75
OS: Win 10
System: Asus i7-7700K 16Gb
Hi,
The variable which holds the random number is 'MyRandoms[MyCount]' so it's already in a variable array. The variable is 'MyRandoms[n]' so to display the first in the series it would be MyRandoms[1].....I think the first element is 1 and not 0.... can't remember for sure.

For some reason which I can't figure out at the moment, when I try to display a dynamic array variable (MyRandoms[MyCount]) in a text box, it won't work, but there's an easy work-around
First you need to create a new variable (MyDisp) in the random script.
first create the variable as a publication variable.(select the publication, then properties, then variables etc)
then in the script add
Code:
MyDisp=   MyRandoms[MyCount]
add this BEFORE MyCount++


To display the random number as they are selected.....create a text box. then right click on the text box and select insert variable from the pulldown menu, then select MyDisp from the pulldown list of variables.


you will also need to add a delay to the random loop so the numbers can be read

I thought a dynamic variable would display in a text box as a constant expression? It will display MyRandoms[1], but not MyRandoms[MyCount] both variables are declared as publication variables.

Sandy

_________________
Whoever designed this, never actually used it!


For this message sandyn 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 29 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