From 6cd63a70f6ecf416485261ee93c25c5f98879566 Mon Sep 17 00:00:00 2001 From: dave Date: Sun, 28 Dec 2003 20:47:56 +0000 Subject: RDoc Struct and random git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@5340 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- random.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'random.c') diff --git a/random.c b/random.c index 86c8198d41..9a843f2aff 100644 --- a/random.c +++ b/random.c @@ -172,6 +172,20 @@ random_seed() return tv.tv_sec ^ tv.tv_usec ^ getpid() ^ n++; } +/* + * call-seq: + * srand(number=0) => old_seed + * + * Seeds the pseudorandom number generator to the value of + * number.to_i.abs. If number is omitted + * or zero, 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. + */ + static VALUE rb_f_srand(argc, argv, obj) int argc; @@ -193,6 +207,26 @@ rb_f_srand(argc, argv, obj) return ULONG2NUM(old); } +/* + * call-seq: + * rand(max=0) => number + * + * Converts max to an integer using max1 = + * max.to_i.abs. If 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 219937-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] + */ + static VALUE rb_f_rand(argc, argv, obj) int argc; -- cgit v1.2.3