From: Bertram Scharpf Date: 2009-12-30T02:55:30+09:00 Subject: Re: Question abour rand() Hi, Am Dienstag, 29. Dez 2009, 18:51:53 +0900 schrieb Panagiotis Atmatzidis: > I'm looking for a way to create a random sequence of numbers using the 'rand' method. I can think of something like: > > ----- > while pipi == TRUE > if x > 1950 || x < 2000 > return x > pipi = FALSE > ------ > > Since however the book that I'm reading has not introduced while/else/if etc. I'm thinking that there must be an easier and probably more sensible way to approach this. As far as I've read rand(2000) will give random numbers from 0-to-2000 no matter what. > > NOTE: I don't want to use another method just to do the job. > > thanks in advance & best regards The "rand" method has been mentioned. Here are just some considerations about the above code. You don't need a loop for calculating a random value. (There is a random algorithm below.) But when use a loop, then you either say "return" or leave it by a condition but not both. cond = true while cond do ... if some_condition then cond = false end ... end _or_ loop do ... break if some_condition ... end Note that "break" can even return values: result = loop do break "hi" end result == "hi" And here is an example of a very simple random implementation: class R def initialize @r = 0 end def nextval @r = ((@r * 0xabcd) + 0x73737) % 0x1_0000_0000 @r >> 16 end end r = R.new new_random_value = r.iterate random_value_below_fifty = r.iterate % 50 Bertram -- Bertram Scharpf Stuttgart, Deutschland/Germany * Discover String#notempty? at .