Not logged inGosu Forums
Forum back to Help Search Register Login
Up Topic Gosu / Gosu Exchange / Random not so random
- - By il mietitore Date 2011-10-08 12:40
In the last years I often listened to conversations regarding the fact that when you randomize a number with a computer, what you get is not a *real* random number. I don't know if that's the case, but I'm having some trouble with a simple random operation.

Here it is:

@snome_random_cont = rand(21)+1
case (@snome_random_cont)
when 18 then begin
                         @snome = "Ogre"
                         @ok = true

Now: what is this? What I am realizing is a simulation of wrestling management. The piece of code i wrote to you is a part of the sequence that creates about 100 initial random wrestlers. Here it's randomizing their nicknames. This part of the code basically WORKS, because all (or almost all) of the other nicknames are chosen, while no wrestler ever had "Ogre" as his nickname.

There shouldn't be that "@ok = true". I put it there just because i saw that the program was never giving that result, so i made a test with that @ok variable. The loop in this case continues until @ok == true. The problem is that the program enters in an infinite loop, just because the random operation never gives 17 as a result. That's bad.

Everyone knows why my ruby don't want to call a wrestler "Ogre"? :(
Parent - - By RavensKrag Date 2011-10-08 16:33
Not sure what you're problem is, but it's not in this block of code.  As usually happens when you get logic errors in programming, the error is not where you might expect it to be.

I ran a simple simulation of this code in IRB and got "ogre"  about 7 times.
Parent - - By il mietitore Date 2011-10-08 19:23

found it.

ahem. the loop was eternal just because i used a wrong type of variable for the test (i wrote @ok, but i should have used $ok, as the loop was in another method >_>).

Ogre people exists, anyway, now i found that that code was ok.

sorry for the nooby thread, problem solved ;)
Parent - - By jlnr (dev) Date 2011-10-08 20:24
If you are using $global variables regularly, you should rethink your design though. @instance_variables are per-object, not per-method, and objects should store references to each other rather than using $globals.
Parent - - By il mietitore Date 2011-10-08 23:24
sorry, my fault, I should have written "the loop was in an other object". That's why I used a global variable.

In fact, I changed all globals in instance variables after you noticed this thing, some days ago, in the glow topic. I haven't had problems up to then, but you know, just to be sure.. :P

One day you will explain me what are the problems with global variables... I'm not sure I understood what are the risks in using the toom much.
Parent - By Spooner Date 2011-10-09 17:07
You are also overusing the syntactic sugar, which I guess is something you've dragged in from another programming language:

case @snome_random_cont
when 18
     @snome = "Ogre"
     @ok = true
when 19

Not a problem really, but Ruby just doesn't need it and keeping it simple can make it easier to read and maintain.

You can probably also simplify even further with (but maybe I'm not seeing other special cases):

@snome = %w[Ogre Geoffrey Fred].sample
@ok = true
Up Topic Gosu / Gosu Exchange / Random not so random

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill