Bug 29645 - Random.NextDouble stops producing random number and returns zero after a while
Summary: Random.NextDouble stops producing random number and returns zero after a while
Status: RESOLVED ANSWERED
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: XI 8.9.x (iOS 8.3)
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-05-01 08:53 UTC by Ed
Modified: 2015-05-01 10:16 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Demo project (161.37 KB, application/zip)
2015-05-01 08:53 UTC, Ed
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Ed 2015-05-01 08:53:11 UTC
Created attachment 11020 [details]
Demo project

I was stress testing ThreadPool to check if it causes issues to my socket connection, and after a while i noticed that Random.NextDouble() in my test just stops producing random numbers. This happens on both iPhone and Simulator. Check the attached project. And see this video: http://www.screencast.com/t/mcC69TXWl

PS: For the ThreadPool, using it for my internal purposes indeed somehow caused lags to my socket connection, as a workaround i simply wrote my own implementation and it worked.
Comment 1 Rolf Bjarne Kvinge [MSFT] 2015-05-01 09:02:55 UTC
You're accessing the Random instance from multiple threads at the same time, and Random isn't thread-safe.

In fact MSDN [1] says that you'll get 0 if you do it: "If you don't ensure that the Random object is accessed in a thread-safe way, calls to methods that return random numbers return 0."

If you change your sample to have one Random per thread, it works fine:

	[ThreadStatic]
	static Random rnd;// = new Random();

	static void OnNSTimerTick(Foundation.NSTimer Timer)
	{
		ThreadPool.QueueUserWorkItem(ThreadPoolCallback);
	}

	static void ThreadPoolCallback(object State)
	{
		if (rnd == null)
			rnd = new Random ();

		for (int i = 0; i < 1000000; i++)
			rnd.NextDouble();

		double rndValue = rnd.NextDouble();

		Console.WriteLine(rndValue.ToString());
	}

[1] https://msdn.microsoft.com/en-us/library/system.random(v=vs.110).aspx
Comment 2 Ed 2015-05-01 10:16:41 UTC
Thanks for pointing this out, my mistake.