Is It Okay To Static-ify Random?

I use


int myRand = new random().nextInt();

if it HAS to be static-ish

(although I am sure many of you are cringing reading this)

That isn’t static though…

It can be used in a static method… Thats all the static I ever need :wink:

The biggest problem with random number generation is people using them is in a bad way. Creating a new generator each time you use one is again one step away from the XKCD code (return 4) above.

(EDIT: typo…again…I’m badly correlated…yuck yuck)

That still isn’t static though. If you create a new instance of your ‘static’ object, except for at runtime, then why use static at all? Just create a new instance of the random class and don’t deal with static at all. Or, do it right and create one instance of the static object for the whole program to call upon.

oh, totally agreed. btw, isnt having a static random almost as bad as having a static method for println?

No, using static is just tricky in general. You use a static reference to println everytime you call System.out.println
If the print method wasn’t static, you’d have to create a new instance of System everytime you’d want to print something out, and that would be a pain in the ass seeing as the console is mainly use for debugging, and these print statements are usually deleted anyway.

Static is tricky because you need to be careful about what you add static to. You should google the theory behind the static keyword. I used to, when I first started out, use static all the time, I just didn’t understand what it was. Now I avoid it if possible like the plague.

What i mean by static println is: I read through someones github code a few days ago and there was a static method called println(String input), that just shortened ‘System.out.println’. Its pretty pointless, much like a static Random(). Just create one, use it as much as possible and where not possible create another. Again, I am sure this is bad practice but it works for me.

You are just doing this because you learned it that way, which isn’t wrong, but game code is special…
Look at libgdx how much of it is static. Intersection checks, file operations, graphic calls, audio calls. LWJGL too of course
In game dev, static/global has a lot of value if you wanna be productive

I am getting into static code more and more, (never used it 6 months ago).

Most ‘utility’ code in my new game is static and I have found it VERY useful to do so. such as my method for getting ‘real’ mouse position (camer.unproject() etc from libgdx).

I am picking up the importance of static code and instantly seeing the benefits.

Well, of course. Static for utility methods is a godsend. Everything else, pretty much no.

On my website, a lot of the code is static. The reason is that I get all the google app engine objects from factories and I have no idea when or how long I can keep references to those objects, so my code is all pretty much stateless static, except for the objects in my data model, which are throwaway instances. If I want to retain a reference to something, I use a stateless static method and the memcache service to store a default return value. There’s a better way to do it, I’m sure, but I don’t have the time or energy at midnight to figure it out.