Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Use radio noise to make (non-critical) decisions...

by dbush (Deacon)
on Jan 08, 2003 at 15:03 UTC ( #225271=CUFP: print w/ replies, xml ) Need Help??

Ever had the problem of not knowing what to do? Perl and random.org to the rescue...

use strict; use warnings; use LWP::Simple; my ($szConumdrum, $szThisSolution, @solution, $URL, $iRandom); print 'Conundrum: '; chomp ($szConumdrum = <STDIN>); while () { print 'Solution: '; chomp ($szThisSolution = <STDIN>); last if $szThisSolution eq ''; push @solution, $szThisSolution; } $URL = "http://www.random.org/cgi-bin/randnum?num=1&min=0&max=$#soluti +on&col=1"; print "Deciding...\n"; $iRandom = get($URL); die "Unable to decide!\n" if not defined $iRandom; print "The solution to '$szConumdrum' is '$solution[$iRandom]'\n"; __END__

I was in two minds about posting this code and so I thought I would ask the script itself. The following is honestly the result...

Conundrum: Should I post this code to PM? Solution: Yes Solution: No Solution: Deciding... The solution to 'Should I post this code to PM?' is 'Yes'

The ether can't be wrong.

Cheers,
Dom.

Update: Changed the code based on jdporter's suggestion in the chatterbox; "you should probably use $#solution, rather than @solution - 1. And you can interpolate it directly".

Comment on Use radio noise to make (non-critical) decisions...
Select or Download Code
Re: Use radio noise to make (non-critical) decisions...
by jdporter (Canon) on Jan 08, 2003 at 15:46 UTC
    That inspired me to try something similiar.....
    # Magic 8 Ball program. use LWP::Simple; my @answers = <DATA>; chomp @answers; my $i = get( "http://www.random.org/cgi-bin/randnum?num=1&min=0&max=$# +answers&col=1" ); defined $i or die "Unable to decide!\n"; print "$answers[$i]\n"; # answers horked from http://www.fiendation.com/people/chris/eight.htm __DATA__ Outlook Good Outlook Not So Good My Reply Is No Don't Count On It You May Rely On It Ask Again Later Most Likely Cannot Predict Now Yes Yes, Definitely Better Not Tell You Now It Is Certain Very Doubtful It Is Decidedly So Concentrate and Ask Again Signs Point to Yes My Sources Say No Without a Doubt Reply Hazy, Try Again As I See It, Yes

    jdporter
    The 6th Rule of Perl Club is -- There is no Rule #6.

      Very neat indeed. In the spirit of TIMTOWTDI...

      #!/usr/bin/perl -w use strict; use warnings; use Tk; use LWP::Simple; #Read the predictions my @answers = <DATA>; chomp @answers; my $szURL = "http://www.random.org/cgi-bin/randnum?num=1&min=0&max=$#a +nswers&co+l=1"; #Initialise state my $szState = 'Starting...'; #Create main window with a canas and bind the mouse event my $main = new MainWindow; my $canvas = $main->Canvas( -height=>300, -width=>300); $canvas->pack( -expand, -fill ); $main->bind('<Button1-ButtonRelease>', \&Display); #Create the fonts my $fontLarge = $canvas->fontCreate( -family=>'arial', -size=>100, -weight=>'bold' ); my $fontSmall = $canvas->fontCreate( -family=>'arial', -size=>10, -weight=>'bold' ); #First time in do the display Display(); MainLoop; sub Display { my ($myFont, $szText, $iPrediction); #Depending on the state do the "right thing" tm. if ($szState eq 'Starting...' or $szState eq 'Prediction') { $szState = 'Displaying 8'; $myFont = $fontLarge; $szText = '8'; } else { $szState = 'Prediction'; $myFont = $fontSmall; $iPrediction = get( $szURL); $szText = $iPrediction ? $answers[$iPrediction] : 'Error!'; } #Over write anything $canvas->createOval( 10, 10, 290, 290, -fill=>'black', -outline =>'black' ); $canvas->createOval( 70, 70, 230, 230, -fill=>'white', -outline =>'white' ); #Put up the message $canvas->createText( 150, 150, -justify=>'center', -text=>$szText, -font=>$myFont, -width=>80 ); } __DATA__ Outlook Good Outlook Not So Good My Reply Is No Don't Count On It You May Rely On It Ask Again Later Most Likely Cannot Predict Now Yes Yes, Definitely Better Not Tell You Now It Is Certain Very Doubtful It Is Decidedly So Concentrate and Ask Again Signs Point to Yes My Sources Say No Without a Doubt Reply Hazy, Try Again As I See It, Yes

      Cheers,
      Dom.

Re: Use radio noise to make (non-critical) decisions...
by insensate (Hermit) on Jan 09, 2003 at 03:25 UTC
    Or...
    use strict; use Quantum::Entanglement; $|++; print "Welcome to the magic 8 ball...type 'leave' to exit\n"; for(;;){ my $answer = entangle(1, 'Outlook Good', 1, 'Outlook Not So Good', 1, 'My Reply Is No', 1, 'Don\'t Count On It', 1, 'You May Rely On It', 1, 'Ask Again Later', 1, 'Most Likely', 1, 'Cannot Predict Now', 1, 'Yes', 1, 'Yes Definately', 1, 'Better Not Tell You Now', 1, 'It Is Certain', 1, 'Very Doubtful', 1, 'It Is Decidedly So', 1, 'Concentrate and Ask Again', 1, 'Signs Point to Yes', 1, 'My Sources Say No', 1, 'Without A doubt', 1, 'Reply Hazy, Try Again', 1, 'As I See It, Yes'); print "What is your question: "; my $ans = <STDIN>; chomp $ans; exit if $ans=~/^leave$/i; print "Rolling"; for(1..4) { sleep 1; print "."; } print "\n$answer\n"; }
      exit if $ans=~/^leave$/i;
      Hmm? Seems overly convoluted to me. exit if "leave" eq lc $ans;
      Update: Also:
      my $answer = entangle(map { 1 => $_ } 'Outlook Good', 'Outlook Not So Good', 'My Reply Is No', "Don't Count On It", 'You May Rely On It', 'Ask Again Later', 'Most Likely', 'Cannot Predict Now', 'Yes', 'Yes Definately', 'Better Not Tell You Now', 'It Is Certain', 'Very Doubtful', 'It Is Decidedly So', 'Concentrate and Ask Again', 'Signs Point to Yes', 'My Sources Say No', 'Without A doubt', 'Reply Hazy, Try Again', 'As I See It, Yes', );
      Just minor niggles of course.

      Makeshifts last the longest.

Re: Use radio noise to make (non-critical) decisions...
by BrentDax (Hermit) on Jan 10, 2003 at 03:33 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://225271]
Approved by boo_radley
Front-paged by boo_radley
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2014-09-21 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (166 votes), past polls