Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
It has to be remembered that random is not the same as 'no repeat'. Truly random sequences will have long strings of identical repeats. To compensate, you'll need some logic to avoid the previous guess. As an example:

#!/usr/bin/perl use warnings; use strict; my @server_list = ( 'server1', 'server2', 'server3', 'server4' ); my @picked = ( 0,0,0,0 ); my $picks = shift; foreach ( 1 .. $picks ) { my $i = int(rand( scalar @server_list )); print "Guess = $i\n"; while ( $picked[$i] ) { $i = int(rand ( scalar @server_list )); print "Guess = $i\n"; }; for my $j ( 0 .. $#server_list ) { if ( $j == $i ) { $picked[$j] = 1; } else { $picked[$j] = 0; } } print "Chosen => ", $i, "\n"; }
And a sample set of results are:

C:\Code>perl no_repeats.pl 5 Guess = 1 Chosen => 1 Guess = 3 Chosen => 3 Guess = 1 Chosen => 1 Guess = 1 Guess = 1 Guess = 0 Chosen => 0 Guess = 0 Guess = 0 Guess = 3 Chosen => 3
In the case of a script which runs from scratch each time, it will need some kind of external access to not repeat itself. That could be a file that keeps a number or name of the last called server, or it could be a randomizing daemon that is persistent and is called by the nonpersistent script. It could be a fifo that is set up initially (if it does not exist) and is read by the script before executing a new random number. Once a random number is used, write it into the fifo.

Update: Added comments on persistence mechanisms.

In reply to Re: Load Balancing by dwm042
in thread Load Balancing by onegative

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 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? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2022-08-16 12:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?