AutoHotkey Community

It is currently May 27th, 2012, 11:44 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 14 posts ] 
Author Message
PostPosted: June 2nd, 2010, 11:51 am 
Offline

Joined: January 10th, 2009, 6:40 pm
Posts: 32
Random.org API function library v0.3

Download
(Library and dependencies in separate files.)
Source (Library and dependencies merged in one file.)
Documentation

Functions:
randomdotorg_integer() - It generates truly random integers in configurable intervals.
randomdotorg_string() - It generates truly random strings of various length and character compositions.
randomdotorg_password() - It generates truly random passwords (6-24 characters long). Uses SSL.
randomdotorg_randomizeList() - It arranges the items of a list in random order.
randomdotorg_lottery() - It allows you to quick pick lottery tickets.
randomdotorg_clocktime() - It generates random clock times of the day (or night).
randomdotorg_sequence() - It randomizes a given interval of integers, i.e., arranges them in random order.
randomdotorg_decimalfraction() - It generates random decimal fractions in the [0,1] interval.
randomdotorg_gaussian() - It generates random numbers from a Gaussian distribution (also known as a normal distribution).
randomdotorg_bitmap() - It generates random bitmaps.
randomdotorg_noise() - It generates random audio noise, i.e., audio files containing perfect white noise.
randomdotorg_quota() - It allows you to examine your quota at any point in time.

New: random bitmap, audio noise, and lottery ticket generator
The bitmap and audio noise functions either return the audio/image binary data in hex format or save the generated audio/image file.

More info in the documentation.

Some of the functions are also documented at http://www.random.org/clients/http/.

RANDOM.ORG wrote:
RANDOM.ORG offers true random numbers to anyone on the Internet. The randomness comes from atmospheric noise, which for many purposes is better than the pseudo-random number algorithms typically used in computer programs.

More about randomness: http://www.random.org/randomness/.

If you're gonna implement one or more of these functions in your script(s) you may wanna read the Guidelines for automated clients at random.org.


Cheers
gahks


Changelog:
v. 0.3
New functions:
randomdotorg_lottery()
randomdotorg_noise()
randomdotorg_bitmap()

v. 0.2
New functions:
randomdotorg_password()
randomdotorg_randomizelist()
randomdotorg_clocktime()
randomdotorg_decimalfraction()
randomdotorg_gaussian()

v. 0.1.1
Minor changes+randomdotorg_string() modified as per sinkfaze's suggestion.


Last edited by gahks on June 4th, 2010, 3:43 pm, edited 3 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 2nd, 2010, 3:37 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
Wow, great. Didn't know for Random.org. :)

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 2nd, 2010, 5:58 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5482
Location: the tunnel(?=light)
A nice sleight of hand on the code, I was thinking there was actual math being performed in AHK! Minor gripe, but you could convert the "on/off" parameters to true/false and allow ternary evaluation to determine the "on/off" status of each parameter while creating the URL. Makes the function a little easier to use and understand, IMO:

Code:
randomdotorg_string(num,len,digits=True,upperalpha=True,loweralpha=True,unique=True,rnd="new") {
   _url :=   "http://www.random.org/strings/?num=" num "&len=" len
       . "&digits=" (digits ? "on" : "off") "&upperalpha=" (upperalpha ? "on" : "off")
       . "&loweralpha=" (loweralpha ? "on" : "off") "&unique=" (unique ? "on" : "off")
       . "&format=plain&rnd=" rnd
. . .

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re:
PostPosted: June 2nd, 2010, 7:18 pm 
Offline

Joined: January 10th, 2009, 6:40 pm
Posts: 32
@majkinetor: It's been around for some time now :)

@sinkfaze: Duly noted, I modified the code according to your suggestion, thanks.
The folks over at random.org do the math, I'm just using the service. :D


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 3rd, 2010, 5:15 pm 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
I have once created a function called TrueRandom() using that, but never managed to upload it anywhere.
Great addition, thanks.

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re:
PostPosted: June 3rd, 2010, 7:42 pm 
Offline

Joined: January 10th, 2009, 6:40 pm
Posts: 32
You're welcome:)

Update: v0.2
New functions:
randomdotorg_password() - It generates truly random passwords (length: 6-24 characters).
randomdotorg_randomizelist() - It arranges the items of a list in random order.
randomdotorg_clocktime() - It generates random clock times of the day (or night).
randomdotorg_decimalfraction() - It generates random decimal fractions in the [0,1] interval.
randomdotorg_gaussian() - It generates random numbers from a Gaussian distribution.

The password generator uses SSL for the query.
More info about the new functions in the Documentation.

Cheers
gahks


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 3rd, 2010, 9:36 pm 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
Althought it doesn't generate true random values, here's
a related function I created some time ago: RandomVar()

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Update
PostPosted: June 4th, 2010, 3:48 pm 
@MasterFocus
Cool, it can come in useful.

Update: v0.3
New functions:
randomdotorg_lottery() - It allows you to quick pick lottery tickets.
randomdotorg_bitmap() - It generates random bitmaps.
randomdotorg_noise() - It generates random audio noise, i.e., audio files containing perfect white noise.

More info in the docs.


Report this post
Top
  
Reply with quote  
 Post subject: Re:
PostPosted: June 4th, 2010, 3:50 pm 
Offline

Joined: January 10th, 2009, 6:40 pm
Posts: 32
And that was me fighting with the login system. :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 4th, 2010, 7:27 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5482
Location: the tunnel(?=light)
randomdotorg_lottery() doesn't output the numbers in a particulary appealing fashion, I revised the function slightly to accommodate a more appropriate output cleanup if you'd like to consider it for the library:

Code:
randomdotorg_lottery(tickets,num_main,highest_main,num_bonus="0",highest_bonus="0") {
   _url :=   "http://www.random.org/quick-pick/?tickets=" tickets "&lottery="
       . num_main "x" highest_main "." num_bonus "x" highest_bonus "&format=plain"
   httpQuery(_ret,_url), VarSetCapacity(_ret,-1)
   If   !InStr(_ret,"Error:") {
      _ret :=   RegExReplace(RegExReplace(_ret,"\b\d\b","0$0"),"\D+/\D+","`t")
      StringReplace, _ret, _ret, `,, `n, All
      StringReplace, _ret, _ret, -, %A_Space%, All
   }   
   return   _ret
}

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re:
PostPosted: June 5th, 2010, 6:10 pm 
Offline

Joined: January 10th, 2009, 6:40 pm
Posts: 32
Yeah, you're right again :).
But in this case the first Stringreplace isn't really necessary: there are no commas in the original format as returned by random.org:
Code:
n-n / n-n (and tickets separated by linefeeds)
Unless it's a typo and you wanted to replace the linefeeds with commas?

Since I originally thought the output of this function will be processed and reformatted to fit any custom needs anyway, I didn't put much effort into ensuring that the returned values are formatted for readability.
But in case someone wants to simply generate a couple of tickets for the next drawing without fussing around with formatting, it's better to output easily readable results.

I'll include the changes in the next update, thanks for the suggestion:)

My English could be better, I hope I'm making sense:)


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re:
PostPosted: June 5th, 2010, 8:08 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5482
Location: the tunnel(?=light)
gahks wrote:
But in this case the first Stringreplace isn't really necessary: there are no commas in the original format as returned by random.org:
Code:
n-n / n-n (and tickets separated by linefeeds)
Unless it's a typo and you wanted to replace the linefeeds with commas?


Actually the output from the lottery has no linefeeds at all, everything is separated by only commas i.e. if you select 3 sets of five regular numbers plus 1 bonus, the output is like this:

Quote:
xx-xx-xx-xx-xx / xx, xx-xx-xx-xx-xx / xx, xx-xx-xx-xx-xx / xx


So in this case I actually reversed it and changed the commas to linefeeds so each set of numbers is on its own unique line:

Code:
StringReplace, _ret, _ret, `,, `n, All

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 6th, 2010, 10:26 am 
Offline

Joined: January 10th, 2009, 6:40 pm
Posts: 32
sinkfaze wrote:
...if you select 3 sets of five regular numbers plus 1 bonus, the output is like this...

I'm not trying to be a smartass, but... :)
That is because the function executed a Stringreplace, replacing all the linefeeds with commas.

The function receives the results from Random.org like this:
Quote:
20-31-43-44-47 / 1
3-9-14-34-43 / 10
4-5-23-40-45 / 17

And it formats it like this by replacing the linefeeds with commas.
Quote:
20-31-43-44-47 / 1,3-9-14-34-43 / 10,4-5-23-40-45 / 17


That first StringReplace in your version of the function does nothing to the data, simply because there are no commas in the data received from Random.org.


Try these three versions of the function:

This one returns the result as it is, without modification:
Code:
randomdotorg_lottery(tickets,num_main,highest_main,num_bonus="0",highest_bonus="0") {
   _url:="http://www.random.org/quick-pick/?tickets=" tickets "&lottery=" num_main "x" highest_main "." num_bonus "x" highest_bonus "&format=plain"
   httpQuery(_ret:="",_url), VarSetCapacity(_ret,-1)
   return _ret
}


This one's copypasted from the library. This produces the format you described:
Code:
randomdotorg_lottery(tickets,num_main,highest_main,num_bonus="0",highest_bonus="0") {
   _url:="http://www.random.org/quick-pick/?tickets=" tickets "&lottery=" num_main "x" highest_main "." num_bonus "x" highest_bonus "&format=plain"
   httpQuery(_ret:="",_url), VarSetCapacity(_ret,-1)
   IfNotInString, _ret, Error:
      StringReplace, _ret, _ret, `n, `,, All ;This Stringreplace replaces the linefeeds from the original format with commas
   return SubStr(_ret,0,1)="," ? SubStr(_ret,1,StrLen(_ret)-1) : _ret ;trimming the last comma
}


And this is your version of the function with the unnecessary StringReplace commented out.
It still works as expected, because there are no commas in the data received from Random.org:
Code:
randomdotorg_lottery(tickets,num_main,highest_main,num_bonus="0",highest_bonus="0") {
   _url :=   "http://www.random.org/quick-pick/?tickets=" tickets "&lottery="
       . num_main "x" highest_main "." num_bonus "x" highest_bonus "&format=plain"
   httpQuery(_ret,_url), VarSetCapacity(_ret,-1)
   If   !InStr(_ret,"Error:") {
      _ret :=   RegExReplace(RegExReplace(_ret,"\b\d\b","0$0"),"\D+/\D+","`t")
      ;StringReplace, _ret, _ret, `,, `n, All
      StringReplace, _ret, _ret, -, %A_Space%, All
   }   
   return   _ret
}


This is the flow of how the third function processes the data:
Code:
Data received from Random.org

20-31-43-44-47 / 1
3-9-14-34-43 / 10
4-5-23-40-45 / 17

Your function

Step 1.
20-31-43-44-47 / 01
03-09-14-34-43 / 10
04-05-23-40-45 / 17

Step 2.
20-31-43-44-47   01
03-09-14-34-43   10
04-05-23-40-45   17

Step 3. (= Step 2.: This StringReplace does nothing)
20-31-43-44-47   01
03-09-14-34-43   10
04-05-23-40-45   17

Step 4.
20 31 43 44 47   01
03 09 14 34 43   10
04 05 23 40 45   17


Cheers :D
gahks


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 6th, 2010, 3:35 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5482
Location: the tunnel(?=light)
Ah, I see what you mean now! I thought it was a little too bizarre for a website to mix up its output in that way to be right.

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: rrhuffy and 58 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group