For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > June 2005 > Re: Randomly selecting Variables









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Randomly selecting Variables
Scott Bryce

2005-06-06, 3:58 pm

George wrote:

> Hi,
> I am sorry and not sure if this query is directly related to perl, but
> since I want to do this trick in perl so
>
> I have a 30 variables out of which I need to randomly select 15 and
> then randomly equate them numbers between 01 and 90,
> Thanks


Your problem definition is confusing. What is the significance of having
exactly 30 variables in the pool, if you want to select 15 out of 90
values? Could you use a hash with 30 keys? (But why, if you only want to
use 15 of them?)

If you want to use 15 numbers randomly selected from a pool of 90
possible values, assign the values to 90 elements in an array, shuffle
the array, pull the first 15 values from the shuffled array.
Anno Siegel

2005-06-06, 3:58 pm

Scott Bryce <sbryce@scottbryce.com> wrote in comp.lang.perl.misc:

[...]

> If you want to use 15 numbers randomly selected from a pool of 90
> possible values, assign the values to 90 elements in an array, shuffle
> the array, pull the first 15 values from the shuffled array.


If it matters, modify the shuffling algorithm to return after 15 steps.
The "already shuffled" elements usually are a compact sub-array that
is easy to return. No need to shuffle the other 75 that aren't looked
at.

Anno
George

2005-06-07, 8:57 am

Scott Bryce wrote:

> George wrote:
>
>
> Your problem definition is confusing. What is the significance of
> having exactly 30 variables in the pool, if you want to select 15 out
> of 90 values? Could you use a hash with 30 keys? (But why, if you
> only want to use 15 of them?)
>
> If you want to use 15 numbers randomly selected from a pool of 90
> possible values, assign the values to 90 elements in an array,
> shuffle the array, pull the first 15 values from the shuffled array.


Those 30 vaiables are used in formating a tabular label defined in
format function , now out of 30 only 15 are assigned in one go , very
much like bingo tickets,
Scott Bryce

2005-06-07, 4:01 pm

George wrote:

> Those 30 vaiables are used in formating a tabular label defined in
> format function , now out of 30 only 15 are assigned in one go , very
> much like bingo tickets,


If you want to randomly assign 15 out of 30 scalars, things could get messy.

Can these 30 variables be stored as hash elements? Instead of $var_1,
$var_2, etc, use $hash{var_1}, $hash{var_2}, etc.

Load an array with the hash keys, shuffle them, then take the first 15
keys from the shuffled array. Assign the random numbers to those hash
elements.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com