(redirected from RandomStep)

[Home]Qetu/RandomStep

Robo Home | Qetu | Changes | Preferences | AllPages

Actual Codesize: 1019 ( MicroBot )
Look what i do! :P

	int randomInt(int range) {
		/* returns random integer in range [0,@range] */
		Double random = new Double(Math.random()*range);
		return random.intValue();
	}

I use it to change my movement randomly across a switch statement ...

Pretty foolish :)


Wouldn't it be clearer to use a Random object?
static Random rand = new Random();
...
... //Wherever you need a random integer:
    randomInt = rand.nextInt(range);

-- PEZ

Interesting! I'll check the difference in codesize and memory. Clearer it is, indeed. That's what i get for being a beginner :P -- Qetu

Hmm, saved 4 bytes. I suppose the memory difference is negligible, so thanks!! -- Qetu

And with a Random object instantiated you have access to cool methods like nextGaussian() too. -- PEZ


Robo Home | Qetu | Changes | Preferences | AllPages
Edit text of this page | View other revisions
Last edited February 22, 2004 20:14 EST by PEZ (diff)
Search: