2

Use this, Rx generates a series of random numbers between 0 and 99.

var R = new Random();
var ints = Observable.Interval(TimeSpan.FromSeconds(1));
var RandomNos = ints.Select(i=> R.Next(100)); // was new Random().Next(100) 
RandomNos.Subscribe(r=> Console.Write(r+ ","));

1,75,49,23,97,71,45,19,93,66,40,14,88,62,36,10,84

I want to capture/detect when I get 6 more-than-50 numbers in a row. Can Rx do it?

RandomNos.?????()
.Subscribe(l=> Console.WriteLine ("You got 6 more-than-50 numbers in a row"));
Rm558
  • 4,621
  • 3
  • 38
  • 43
  • 3
    You should not be constantly creating new `Random` instances. Instead create one and call `Next` on it repeatedly. – Servy Mar 02 '15 at 16:04

2 Answers2

4

One way to do this is with the Buffer method.

var random = new Random();
var result = Observable.Interval(TimeSpan.FromSeconds(1))
    .Select(i => random.Next(100))
    .Buffer(6, 1)
    .Where(buffer => buffer.All(n => n > 50))

If instead of 6-in-a-row you were trying to detect K-in-a-row, where K was really really huge, then you'd probably want to do something using Window instead, but since K = 6 here it's easiest to just do what I suggested.

Also, be aware that the probability of a number drawn uniformly from {0, 1, ..., 99} being greater than 50 is 49/100, not 1/2.

Timothy Shields
  • 75,459
  • 18
  • 120
  • 173
2

Just for fun, here's one that only uses a single counter for any "n" in a row - it keeps a running count of numbers over 50 - the Take(1) completes the stream at the first occurrence.

RandomNos.Scan(0, (a,x) => x > 50 ? ++a : 0)
         .Where(x => x == 6)
         .Take(1)
         .Subscribe(_ => Console.WriteLine("You got 6 more-than-50 numbers in a row"));
James World
  • 29,019
  • 9
  • 86
  • 120
  • Note that Tim's answer is O(1) in memory too. – Servy Mar 02 '15 at 20:28
  • Tim's answer is also O(1) in that respect. – Servy Mar 02 '15 at 20:29
  • He needs a buffer of size [0,6]. That's O(1), because the size of the buffer is always less than a constant amount (6). – Servy Mar 02 '15 at 20:30
  • I think I already admitted I was wrong about the O(n)... I am focussing on the fact that for say, 1 million in a row, I have a single counter, whereas he needs a million buffers. You also missed the fact there are multiple buffers.... really the -1 is a bit harsh. – James World Mar 02 '15 at 20:31
  • But the question doesn't require a million consecutive items, it requires 6, set as a constant. – Servy Mar 02 '15 at 20:33
  • You are right Servy... well done. Silly me for suggesting an interesting aside along with an alternative answer. Especially one addressing a point that Tim brought up himself! – James World Mar 02 '15 at 20:33
  • You haven't suggested a corollary. Your proposal does not follow from what is already proven. – Servy Mar 02 '15 at 20:35
  • Well, hopefully someone else will find this of interest! :) Tell you what, I'll leave it here just in case. – James World Mar 02 '15 at 20:35
  • 4
    Your answer is interesting & helpful. – Rm558 Mar 03 '15 at 05:58
  • 1
    @JamesWorld - Very nice use of `Scan`. That's one useful operator - very good at introducing "local" state. – Enigmativity Mar 04 '15 at 02:11