Creating a system that generates a random seed and resets it everyday at the same time

Hi,
I’m quite new with Unity Cloud Code and just could not find an answer so I’m sorry if this is a stupid question.

Is it possible to use Unity Cloud Code (or some other Unity Cloud services) to create a system that generates a random seed and resets it every day and stores it somewhere where all players can access it in the game?

I would just have a date-based seed.

For my Royal Mazes game I do this for two things:

  • when you do the Daily Challenge Maze, everybody has the same seed for a given date. I randomly chose this formula:
static int GetDailyChallengeSeed( DateTime currentTime)
{
	int day = currentTime.Day;
	int month = currentTime.Month;
	int year = currentTime.Year;

	int seed = year * 400 + month * 32 + day;

	return seed;
}

and I assign that to Random.seed before generating the maze.

  • I also implemented a head-to-head feature where two people simultaneously press SHARE SYNC and it runs this code to produce the identical value on two devices:
static int GetUnixTime()            // ChatGPT
{
	// Define the Unix epoch (January 1, 1970 00:00:00 UTC)
	DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

	// Get your DateTime (ensure it's in UTC)
	DateTime currentTime = DateTime.UtcNow;  // You can replace this with any DateTime

	// Calculate the difference between the two DateTime objects
	TimeSpan timeSpan = currentTime - unixEpoch;

	// Get the total seconds since Unix epoch
	long unixTime = (long)timeSpan.TotalSeconds;

	return (int)unixTime;
}

and divides the seconds by 60:

int seed = GetUnixTime();
seed /= 60;

To cross-verify visually, it modulos that number with 60 and calls it a “Shared Minute ##” in the pre-game UI.

That way if we both have the same shared minute (easy to see at a glance), a number which is the same as what the clock says, then we both started with the same seed and we see the same maze while we race.

NOTE: throughout all of this, no connection is made to any server and no data is transferred between either phone. This is just for a shared identical initial maze, either Daily or Share Sync.

EDIT: Oh yes: Royal Mazes is a brand-new mode in my KurtMaster2D game package. It is pending release on iPhone (any moment now) but it is fully out on Android.

Apple iTunes: ‎KurtMaster2D on the App Store (pending)
Google Play (including TV): https://play.google.com/store/apps/details?id=com.plbm.plbm1

Hi, thanks for the answer. I’m still a bit confused if this system doesn’t get time from the server. How can you assure that both players devices have the correct time set?

I don’t concern myself with it. :slight_smile:

Well you see this is exactly what I am considered with and I am specifically asking how can I get the server to reset the seed at the same time every day. This type of system is not reliable for my purposes.

Thank you anyway.

1 Like

You just need the time then. You can use any NTP service to get an accurate time.

Whether you generate the seed from the date on the client device or have the client device receive the seed from a server, the client’s device can be hacked to use a different seed instead. If you mean cheat-safety by “reliable” then you’d have to run the entire logic on the server and return the result, not the seed.

I feel so dumb… Of course, all I need is the time and I can then create the seed in the client! Thank you and my apologies :smiley:

Best practice when accessing NTP to get time is to use more than one NTP server in case of a temporary service downtime.

Also you need to decide whether you want to use the device time if the user has no Internet, or not allow to play without Internet - this may have implications on approval because if there’s no obvious need for the app to be online, it may get rejected (by Apple, of course).