in reply to Help Me in following code ::

i want groups of six random no. which total of services must be in between 840 - 900 But problem is that i am getting repeating random No. & that is i don want

There are several ways to interpret that.

One way is that you want: groups of six randomly chosen, unique (positive) integers that sum to between 840 - 900 (inclusive?). Here is a sub that will do that:

#! perl -slw use strict; use List::Util qw[ sum ]; sub nUniqRandsSumTo { my( $n, $target ) = @_; ## Prevent infinite loop. die 'No solution' if ( ( $n * ($n-1) ) / 2 ) > $target; my @rvs; { @rvs = $target; while( @rvs < $n ) { ## pick an element at my $i = int rand @rvs; ## Can't split 0, 1, 2 into two unique values redo if ( my $toSplit = $rvs[ $i ] ) < 3; my $first = int( rand $toSplit ); my $second = $toSplit - $first; ## try again if it split into two equal values redo if $first == $second; ## replace the original with the random pair splice @rvs, $i, 1, $first, $second; } my %seen; ## Do it all from scratch if we got dups ## Can be expensive if the target is close to the limit redo if grep{ ++$seen{ $_ } > 1 } @rvs; } return @rvs; } for( 1 .. 10 ) { my $target = 840 + int( rand 61 ); my @rands = sort{ $a <=> $b } nUniqRandsSumTo( 6, $target ); printf "sum: %3d [ %s ]\n", sum( @rands ), "@rands"; } __END__ c:\test>834245.pl sum: 896 [ 0 27 34 162 197 476 ] sum: 889 [ 35 77 109 179 196 293 ] sum: 878 [ 18 23 77 91 280 389 ] sum: 885 [ 6 37 68 136 154 484 ] sum: 854 [ 7 8 12 70 237 520 ] sum: 899 [ 8 10 29 101 293 458 ] sum: 858 [ 0 28 37 52 367 374 ] sum: 887 [ 1 8 11 22 43 802 ] sum: 899 [ 11 64 65 117 271 371 ] sum: 872 [ 7 14 40 43 154 614 ]

The way it works is it picks a target between 840 & 900 and then calls the sub with N (6) and the chosen target.

The sub:

  1. Places the target in an array.
  2. Loops while the size of the array is less than N.
  3. Picks one of the values in the array at random and splits it randomly into 2 values that add up to the original.
  4. Checks for dups and starts again if it has any.

Other possible interpretations include:


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"I'd rather go naked than blow up my ass"

Replies are listed 'Best First'.
Re^2: Help Me in following code ::
by gskoli (Novice) on Apr 13, 2010 at 08:31 UTC
    Thanks ....
    But the range should now not be exclude ...
    In the o/p the group of no. must be in range that is we have defined that means ::: Actually The array look like this
    New Lots   Serv.
    1   121
    2   182
    3   111
    4   160
    5   105
    6   113
    7   121
    8   97

    Here "New Lots" having range of (186) Respected Services are given
    So now i want Such "New Lots" with group of 4/5/6 in such way that it will not be repeated it will not contain zero and respected "Serv." addition must be in between 840-900

      Sorry. But I do not understand this description at all. Maybe someone else will.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Ok let me explain again
        I have stated array above @A_services that value must be from that as well as Random number from rand function must not be zero
        as well as no repeat random no.
        Please go through with my post again and help me out
        The range of Random Number is from 1-186 the number is only from range .And respected number having value is from Array @A_services
        e.g.
        Suppose first 6 number are 1,2,3,4,5,6 now
        find its value from @A_services that is $A_servises1 that will be 121
        now add all values from array and its addition must be in between 840 - 900
        also that 6 number must not repeat any where .
        Please Give a look again