Jump to content
Sign in to follow this  
download500

Random numbers not random

Recommended Posts

I'm having some issues with some of my random numbers always being the same. I did some digging and realized that it's an issue in C# when you have too many NEW randoms close together because they use the same seed.

I have currently been using this:

int cophelps = new Random.Next(1, 3);

int suspectAttacks = new Random.Next(1,9);

however I am assuming I need to switch to:

int cophelps =  myRand.Next(1, 3);

int suspectAttacks = MyRand.Next(1, 9);

 

with private Random myRand; up top with the peds/vehicles etc at first time declaration. Is that correct?

Edited by download500

Share this post


Link to post
Share on other sites
22 minutes ago, tanu1215 said:

You could just sleep the fiber for like.. 5ms in between.

Question about that as well. When I have slept the fiber it seems to pause the whole system until sleep is complete.

Can I have unlimited sleeps (that wont affect other processes, if I use 

 

            GameFiber.StartNew(delegate
                {
                    GameFiber.Sleep(5000);

?

 

If so, can I make it GameFiber.StarNew(Delegate each time, or do I have to call it something different every time?

Edited by download500

Share this post


Link to post
Share on other sites
1 hour ago, download500 said:

Question about that as well. When I have slept the fiber it seems to pause the whole system until sleep is complete.

Can I have unlimited sleeps (that wont affect other processes, if I use 

 


            GameFiber.StartNew(delegate
                {
                    GameFiber.Sleep(5000);

?

Well, if you're only using GameFiber.Sleep() to separate the seeds, then it shouldn't really be a problem as you're only sleeping for 500 milliseconds, which is half a second. You could sleep even less than that, because I think seeds go down to the millisecond (correct me if I am wrong).

But yes, start a new fiber for when you want to sleep.

It will keep from other things not being processed.

 

Edited by tanu1215

Share this post


Link to post
Share on other sites
1 hour ago, download500 said:

however I am assuming I need to switch to:


int cophelps =  myRand.Next(1, 3);

int suspectAttacks = MyRand.Next(1, 9);

 

with private Random myRand; up top with the peds/vehicles etc at first time declaration. Is that correct?

Yep, this is the way to go.

33 minutes ago, download500 said:

Question about that as well. When I have slept the fiber it seems to pause the whole system until sleep is complete.

Can I have unlimited sleeps (that wont affect other processes, if I use 

 


            GameFiber.StartNew(delegate
                {
                    GameFiber.Sleep(5000);

?

 

If so, can I make it GameFiber.StarNew(Delegate each time, or do I have to call it something different every time?

Yeah, if you're wanting to sleep you should start a fresh GameFiber. The 'delegate' is the method used for starting the gamefiber, not its actual name, though you can name a GameFiber like so:

 

GameFiber.StartNew(delegate
	{
		GameFiber.Sleep(5000);
		Game.LogTrivial("I'm a sleepy fiber");
	}, "Sleepy Fiber Name");

 

 

 

Although I haven't quite figured out if this is actually useful in any way (though it does appear to at least show the name of the active fiber in logs, which can be good for troubleshooting)

Edited by Darkmyre
code formatting

Share this post


Link to post
Share on other sites

You could also use a static random instance with a semi-random seed (system tick count for example). This way calling the function again in short period of time will get you different results because you don't initialize a new instance every call.

Share this post


Link to post
Share on other sites
4 hours ago, LMS said:

You could also use a static random instance with a semi-random seed (system tick count for example). This way calling the function again in short period of time will get you different results because you don't initialize a new instance every call.

What he said. If you create new instances of Math.Random within a short period of time, you are going to get the same results.

Instead, create one global static instance of Random, and use it everywhere in your plugin.

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  

×