• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
hpdvs2

lighter Random function

8 posts in this topic

This is related to C#/Unity, but it boils down to simple math/loops/shifts.

 

Here is the basic idea of the random functions I have:  

 

public float GetRand(int seed, int x, int y) {

  return (float)(new Random(seed + x + (y <<16)).NextDouble();

}

 

public int GetRand(int seed, int x, int y, int range) {

  return (new Random(seed + x + (y <<16)).Next(range);

}

 

The problem is that when the client side starts, its calling both these functions about a million times  (1024x1024 tile buffer), which means that I generate 1,048,576 new random objects that are immediately discarded.

 

I'm using this to determine the height and tile type of each tile on a game map.  The server and the client will need to know the same information, but from the map sizes, that is easily 50 megs of data.  Instead of transferring this data from server to client, and requiring the server to maintain this data for every user, when I need to know a tile height/position, I simply rely on this.

 

The client needs to do this in bulk, to preload an area of the map.  The server is a restful API, and usually doesn't care about specific terrain.  but when it does, I simply have it request a rectangle of the height map or a region of the tile type map, which is generated on the fly by these functions.

 

What I would like, is to reduce the function down to a smaller math example to get a pseudo random value instead of producing/depending on new random objects.

 

So far, my existing functions work, and the speed is not bad on client or server, but it seems like an area that could get a quick boost in speed, and I'll need that as I add more features.

Edited by Dan Violet Sagmiller
0

Share this post


Link to post
Share on other sites

You're using the Random class wrong. You should create an instance once, at startup, with a seed, and then continously call next on that instance, instead of creating a new one for each call.

 

You are not supposed to change the seed each call, and the coordinates you send in will just mess up the sequence for the random number generator, and make it produce less wellbehaved random result.

 

Using it properly should speed it up a lot

 

If you want reproducable result, you just need to save one seed for each level

Edited by Olof Hedman
1

Share this post


Link to post
Share on other sites
What Olof said. Also, if you want to be able to compute the number for each x, y without going through the whole sequence (which your current code allows but Olof's suggestion wouldn't), what you want is a hash function. Your current code is abusing the PRNG as a hash function, which is less than ideal (and probably very slow).

See if this is fast enough and random enough for you:
public uint GetHash(int seed, int x, int y) {
  uint hash = seed;
  hash ^= x;
  hash *= 0x51d7348d;
  hash ^= 0x85dbdda2;
  hash = (hash << 16) ^ (hash >> 16);
  hash *= 0x7588f287;
  hash ^= y;
  hash *= 0x487a5559;
  hash ^= 0x64887219;
  hash = (hash << 16) ^ (hash >> 16);
  hash *= 0x63288691;
  return hash;
}

public float GetRand(int seed, int x, int y) {
  return (float)GetHash(seed, x, y) / 4294967296.0f;
}

public int GetRand(int seed, int x, int y, int range) {
  return GetHash(seed, x, y) % range;
}
Edited by Álvaro
1

Share this post


Link to post
Share on other sites

@olof Hedman:

 - I would agree, accept that you should think of this more like Minecraft on a restful service.  The world map is virtually infinite (2 billion tiles in any direction)  .   I.e. it is not a level.  I could section it off.  but this is ultimately more effective.  If the server has to pull up data to figure something out, I don't want to have to pull entire sections, especially if something is on the kusp, and in order to validate the position of 4 tiles, it has to pull up 4 maps of 64x64.  In this case, most of the processing will be for 12 tiles at a time or less on the server.  I want the server to be able to handle hundreds of clients.  Since it is a restful service, it won't be retaining live connections or session data for anyone.  The only thing the server holds in memory is a single sessionkey/id to make sure the source is valid and maintain a single sign in.

 

Also, Microsoft's Random, uses math code that loops an equation 110 times, half of which contain an if statement.  seems horribly inefficient to use random even the way you suggest. 

 

@Alvaro:

THANK YOU!!!!  This code does exactly what I need, and is more efficient than Microsoft's.  

0

Share this post


Link to post
Share on other sites

@olof Hedman:

 - I would agree, accept that you should think of this more like Minecraft on a restful service.  The world map is virtually infinite (2 billion tiles in any direction)  .   I.e. it is not a level.  I could section it off.  but this is ultimately more effective.  If the server has to pull up data to figure something out, I don't want to have to pull entire sections, especially if something is on the kusp, and in order to validate the position of 4 tiles, it has to pull up 4 maps of 64x64.  In this case, most of the processing will be for 12 tiles at a time or less on the server.  I want the server to be able to handle hundreds of clients.  Since it is a restful service, it won't be retaining live connections or session data for anyone.  The only thing the server holds in memory is a single sessionkey/id to make sure the source is valid and maintain a single sign in.

 

Also, Microsoft's Random, uses math code that loops an equation 110 times, half of which contain an if statement.  seems horribly inefficient to use random even the way you suggest. 

 

@Alvaro:

THANK YOU!!!!  This code does exactly what I need, and is more efficient than Microsoft's.  

Well, the way you use random makes it much less random (if random at all - your GetRand() is basically a simple f(x,y) ).

 

You're seeding every time you create a new Random object.

As far as i know, adding up seeds seeds and using the first result makes the result more predictable than following a sequence (Like alvaro said; "a less wellbehaved random result").

Regardless, the Random object could be allocated once, and then re-seeded later.

This alone would speed up things a lot.

 

Also, try storing the random bits of your world in larger chunks, if they really need to divert in seed.

I think you shouldn't need that many seeds, though - Think it through again.

 

(Alvaro's approach is very nice for addressing, but its usage in your code should be founded on a solid address-value principle. You want to be able to fetch the values dynamically, even from places far away. The addressing has little to do with randomization. (And therefore has nothing to do with Microsoft's random being "slow".))

Edited by SuperVGA
0

Share this post


Link to post
Share on other sites

On time tests between Random, and the code @Alvaro provided:

 

1,000,000 loops

new Random(seed).NextDouble() = 3900+ ms constantly

Alvaro's float GetRand = < 67 ms constantly

 

Much better perf.

Edited by Dan Violet Sagmiller
0

Share this post


Link to post
Share on other sites

here is the random code I use for this:

 

 

        public static uint GetHash(int seed, int x, int y)
        {
            uint hash = (uint)seed;
            hash ^= (uint)x;
            hash *= 0x51d7348d;
            hash += 0x85dbdda2;
            hash = (hash << 16) ^ (hash >> 16);
            hash *= 0x7588f287;
            hash ^= (uint)y;
            hash *= 0x487a5559;
            hash += 0x64887219;
            hash = (hash << 16) ^ (hash >> 16);
            hash *= 0x63288691;
            return hash;
        }


        public static float GetRand(int seed, int x, int y)
        {
            return (float)GetHash(seed, x, y) / 4294967296.0f;
        }


        public static int GetRand(int seed, int x, int y, int range)
        {
            return (int)(GetHash(seed, x, y) % range);
        }
 

 

 
It doesn't actually need to be as Random as provided by System.Random.  I need it to have the general shift to disguise obvious patterns related to X and Y coordinates.  I just wasn't finding the best way to describe that.
 
A point we have to ask our selves, is how "random" do we need it to be?  I've looked through the results I've gotten back, and so far, everything appears nice and random, without a significantly noticable pattern.  Once I apply this to the X/Y grid, that might change though.
 
Thanks for your assistance.
0

Share this post


Link to post
Share on other sites
It looks like you copied my code immediately after I posted it. I realized then that I accidentally posted "+=" in a couple of lines where I means "^=". I have some evidence to believe that "^=" makes the generated numbers much more random looking, and the performance shouldn't be much slower.

I just wanted to say this before someone posts a picture showing obvious patterns in the numbers generated with that code (which do exist).
0

Share this post


Link to post
Share on other sites

actually, the pattern works pretty well in my case, I'm using the int range to select the tile image, and I'm using the float to set the height.  

*please note: this texture setup is does not have setups smooth transitions yet.   This is only intended to show the randomization via the new code.  

 

03.27.2013-13.30.png

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0