Class: Random

Inherits:
NSObject show all

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from NSObject

#!, #!=, #!~, #, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version

Constructor Details

- (Object) new([seed])

Creates new Mersenne Twister based pseudorandom number generator with seed. When the argument seed is omitted, the generator is initialized with Random.new_seed.

The argument seed is used to ensure repeatable sequences of random numbers between different runs of the program.

prng = Random.new(1234)
[ prng.rand, prng.rand ]   #=> [0.191519450378892, 0.622108771039832]
[ prng.integer(10), prng.integer(1000) ]  #=> [4, 664]
prng = Random.new(1234)
[ prng.rand, prng.rand ]   #=> [0.191519450378892, 0.622108771039832]

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class NSObject

Class Method Details

+ (Object) alloc

:nodoc:

+ (Object) left

:nodoc:

+ (Integer) new_seed

Returns arbitrary value for seed.

Returns:

+ (Numeric) rand(max = 0) + (Float) rand + (Numeric) rand(limit)

If max is Range, returns a pseudorandom number where range.member(number) == true.

Or else converts max to an integer using max1 = max.to_i.abs.

Then if max is nil the result is zero, returns a pseudorandom floating point number greater than or equal to 0.0 and less than 1.0.

Otherwise, returns a pseudorandom integer greater than or equal to zero and less than max1.

Kernel::srand may be used to ensure repeatable sequences of random numbers between different runs of the program. Ruby currently uses a modified Mersenne Twister with a period of 2**19937-1.

srand 1234                 #=> 0
[ rand,  rand ]            #=> [0.191519450163469, 0.49766366626136]
[ rand(10), rand(1000) ]   #=> [6, 817]
srand 1234                 #=> 1234
[ rand,  rand ]            #=> [0.191519450163469, 0.49766366626136]

Overloads:

+ (Object) srand(number = 0)

Seeds the pseudorandom number generator to the value of number. If number is omitted, seeds the generator using a combination of the time, the process id, and a sequence number. (This is also the behavior if Kernel::rand is called without previously calling srand, but without the sequence.) By setting the seed to a known value, scripts can be made deterministic during testing. The previous seed value is returned. Also see Kernel::rand.

+ (Object) state

:nodoc:

Instance Method Details

- (Boolean) ==(prng2)

Returns true if the generators' states equal.

Returns:

  • (Boolean)

- (String) bytes(size)

Returns a random binary string. The argument size specified the length of the result string.

Returns:

- (Object) initialize_copy

:nodoc:

- (Object) marshal_dump

:nodoc:

- (Object) marshal_load

:nodoc:

- (Float) rand - (Numeric) rand(limit)

When the argument is an Integer or a Bignum, it returns a random integer greater than or equal to zero and less than the argument. Unlike Random.rand, when the argument is a negative integer or zero, it raises an ArgumentError.

When the argument is a Float, it returns a random floating point number between 0.0 and max, including 0.0 and excluding max.

When the argument limit is a Range, it returns a random number where range.member?(number) == true.

prng.rand(5..9)  #=> one of [5, 6, 7, 8, 9]
prng.rand(5...9) #=> one of [5, 6, 7, 8]
prng.rand(5.0..9.0) #=> between 5.0 and 9.0, including 9.0
prng.rand(5.0...9.0) #=> between 5.0 and 9.0, excluding 9.0

begin/end of the range have to have subtract and add methods.

Otherwise, it raises an ArgumentError.

Overloads:

- (Integer) seed

Returns the seed of the generator.

Returns: