Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I have a piece of code that has worked a long time, but I know there has got to be a better way to do this. I dislike gotos but that was the best I could come up with at the time. Any help would be much appreciated! Thanks in advance! My goal is to get the most random selection of items from the @weighteddiv array to the @selected array without duplicating common numbers, i.e. the @selected array can not have a duplicate number. The values are int 4 digits, like 2345. The @weighteddiv will have 1...N numbers at various frequencies, like 2200 of 2161, 1854 of 3222, 50 of 2344 and so forth. Hopefully this clears up more of what this code is suppose to do.
use List::Util shuffle; @weighteddiv = shuffle @weighteddiv; @selected = (); foreach $i (0..$maxclients-1) { xyz: if ($#weighteddiv > = 0) { $idx=int(rand($#weighteddiv)); $selected[$i] = $weighteddiv[$idx]; if ($i>0) { foreach $j (0..$i-1) { if ($selected[$i] == $selected[$j]) { goto xyz } } } } }

In reply to removing the goto by scoobyrico

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (1)
As of 2024-04-19 00:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found