Bug 26836 - Random Next returns sequences
Summary: Random Next returns sequences
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: System (show other bugs)
Version: 3.12.0
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2015-02-07 23:58 UTC by blaize
Modified: 2015-02-24 20:24 UTC (History)
1 user (show)

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


Description blaize 2015-02-07 23:58:46 UTC
using System;

namespace TestRNG
    class MainClass
        public static void Main (string[] args)
            Random r = new Random (Guid.NewGuid().ToByteArray()[0]);
            for (int i = 0; i < 20; i++) {
                int rn = r.Next ();
                Console.WriteLine(rn + " " + (rn % 2));

Always returns the same set of values for (rn % 2) even though rn is different.  These values are the same as Random(0, 2) returns.  I get these values: 00111111110110001001.

bolt:~$ mono --version
Mono JIT compiler version 3.12.0 (tarball Mon Jan 19 14:55:43 UTC 2015)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
	TLS:           __thread
	SIGSEGV:       altstack
	Notifications: epoll
	Architecture:  amd64
	Disabled:      none
	Misc:          softdebug 
	LLVM:          supported, not enabled.
	GC:            sgen
bolt:~$ uname -a 
Linux bolt 3.13.0-24-generic #47-Ubuntu SMP Fri May 2 23:30:00 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

I know we used to see this kind of thing with taking rand() in c modulus some number, in the old days... but I would have thought that we were beyond that now.  Certainly the source looks to be implementing something more sophisticated.
Comment 1 blaize 2015-02-24 20:24:47 UTC
Note that this is also the case for 

Random r = new Random ();

Note You need to log in before you can comment on or make changes to this bug.