Jump to content

Can someone make a Random UDF


Recommended Posts

Here you go. You can donate using PayPal directly to josvanegmond at gmail dot com

#include <INet.au3>

$s = _Random(0, 1000)
MsgBox(0,"", $s)

Func _Random($min, $max)
    Return _INetGetSource("http://www.random.org/integers/?num=1&min=" & $min & "&max=" & $max & "&col=1&base=10&format=plain&rnd=new")
EndFunc
Edited by Jos
changed email to avoid spam
Link to comment
Share on other sites

@Manadar,

If you were/are serious about making a UDF to use this site, then you might want to build in the features needed to make the UDF comply with this info...

http://www.random.org/clients/

http://www.random.org/clients/http/#quota

...so as not to get the user of the UDF IP banned:

http://www.random.org/banned-hosts/

Not that it seems to be a giant problem. :-)

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

Opt("MustDeclareVars", 1)

Global Const $multiplier = 16807
Global Const $increment = 0
Global Const $modulus = _Pow(2, 31) - 1
Global $seed

Func _SetSeed($value)
    $seed = $value
EndFunc

Func _NextSeed()
    $seed = Mod($multiplier * $seed + $increment, $modulus)
    Return $seed
EndFunc

Func _Pow($base, $exp)
    Local $result = 1
    For $i = 1 To $exp
        $result *= $base
    Next
EndFunc

Just use _SetSeed when priming the random number generator, then keep calling _NextSeed to generate numbers.

Code is for basic LCG using Apple Carbon parameters. http://en.wikipedia.org/wiki/Linear_congruential_generator

Number range is [0, 2^31 - 1).

If you want it unit random, [0, 1), get the decimal result of (_NextSeed() / $modulus).

That should be enough for basic needs. Don't ask for a Mersenne Twister... x_x

Edited by omikron48
Link to comment
Share on other sites

Opt("MustDeclareVars", 1)

Global Const $multiplier = 16807
Global Const $increment = 0
Global Const $modulus = _Pow(2, 31) - 1
Global $seed

Func _SetSeed($value)
    $seed = $value
EndFunc

Func _NextSeed()
    $seed = Mod($multiplier * $seed + $increment, $modulus)
    Return $seed
EndFunc

Func _Pow($base, $exp)
    Local $result = 1
    For $i = 1 To $exp
        $result *= $base
    Next
EndFunc

Just use _SetSeed when priming the random number generator, then keep calling _NextSeed to generate numbers.

Code is for basic LCG using Apple Carbon parameters. http://en.wikipedia.org/wiki/Linear_congruential_generator

Number range is [0, 2^31 - 1).

If you want it unit random, [0, 1), get the decimal result of (_NextSeed() / $modulus).

That should be enough for basic needs. Don't ask for a Mersenne Twister... x_x

This is NOT true randomness but nice work!
Link to comment
Share on other sites

If you want true randomness, go stare at a radioactive isotope... >_<

Yeah, the code I posted is only for psuedo rng, but that's what computing power is for. Most random number generation done on computers use pseudo random number generators, due to ease of replication for testing and verification.

Edited by omikron48
Link to comment
Share on other sites

If you want true randomness, go stare at a radioactive isotope... >_<

Yeah, the code I posted is only for psuedo rng, but that's what computing power is for. Most random number generation done on computers use pseudo random number generators, due to ease of replication for testing and verification.

This is just a proof of concept and adding a UDF to the community. It does not need to have a reason.

Link to comment
Share on other sites

Ah. I have C# code for LCG, MRG and Mersenne Twister.

I'm currently just not in the mood to convert it into AutoIt script. It's kind of messy translating OO code to procedural since it's no longer encapsulated, thus not as fault proof.

For twister, you need 32-bit uint(s) for bit shifting.

C Rand() sucks. Even that basic LCG gives better numbers than Rand().

If you want random numbers, you're better off coding your own rng than relying on the ones readily available. At least you know what you're getting.

Link to comment
Share on other sites

Ah. I have C# code for LCG, MRG and Mersenne Twister.

I'm currently just not in the mood to convert it into AutoIt script. It's kind of messy translating OO code to procedural since it's no longer encapsulated, thus not as fault proof.

For twister, you need 32-bit uint(s) for bit shifting.

C Rand() sucks. Even that basic LCG gives better numbers than Rand().

If you want random numbers, you're better off coding your own rng than relying on the ones readily available. At least you know what you're getting.

Can you send me the code? I will translate it, I got nothing to do.

Link to comment
Share on other sites

You're requesting a Random UDF because you're lazy, but at the same time you have nothing to do and start more projects. This strikes me as odd.

Let's see. I did not write in Autoit mainly for almost a month now and I feel that the syntax is hard so I felt lazy. But when the word C#(my main language other c++ now) I felt like fun >_<

Link to comment
Share on other sites

You're requesting a Random UDF because you're lazy, but at the same time you have nothing to do and start more projects. This strikes me as odd.

And after I invested all that time in it too !!!

@athiwatc, the bill's in the mail.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

... Don't ask for a Mersenne Twister... x_x

From the help file under the Random function:

Comments based on the original source

This function uses the Mersenne Twister random number generator, MT19937, written by Takuji Nishimura, Makoto Matsumoto, Shawn Cokus, Matthe Bellew and Isaku Wada.

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

From the help file under the Random function:

Cool. Then I don't need to bother with the twister then.

I wouldn't know since I have yet to need random numbers in my AutoIt scripting so I have yet to investigate any available random functions.

But a question though, how is the twister queue populated by the code?

The thing with Mersenne twister is that it produces good random numbers, but if you don't use another rng to initially populate the array it uses for generating numbers, the random numbers it spits out at the start are bad quality. Mesenne twister quality improves after a lot of generates but sucks at the initial. LCG on the other hand is good at the start but starts declining when called a lot of times.

Link to comment
Share on other sites

...

But a question though, how is the twister queue populated by the code? ...

I have no clue, but the Random func has been a part of AutoIt long enough that the source code can probably be found in the info from this post:

http://www.autoitscript.com/forum/index.php?showtopic=8389

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...