Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / libs / c5 / UserGuideExamples / RandomSelection.cs @ 3

1
/*
2
 Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
3
 Permission is hereby granted, free of charge, to any person obtaining a copy
4
 of this software and associated documentation files (the "Software"), to deal
5
 in the Software without restriction, including without limitation the rights
6
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
 copies of the Software, and to permit persons to whom the Software is
8
 furnished to do so, subject to the following conditions:
9
 
10
 The above copyright notice and this permission notice shall be included in
11
 all copies or substantial portions of the Software.
12
 
13
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
 SOFTWARE.
20
*/
21

    
22
// C5 example: RandomSelection.cs for pattern chapter
23

    
24
// Compile with 
25
//   csc /r:C5.dll RandomSelection.cs 
26

    
27
using System;
28
using C5;
29
using SCG = System.Collections.Generic;
30

    
31
namespace RandomSelection {
32
  class RandomSelection {
33
    public static void Main(String[] args) {
34
      ArrayList<int> list = new ArrayList<int>(), copy1, copy2;
35
      list.AddAll(new int[] { 2, 3, 5, 7, 11, 13, 17, 19 });
36
      copy1 = (ArrayList<int>)list.Clone();
37
      copy2 = (ArrayList<int>)list.Clone();
38
      const int N = 7;
39
      Console.WriteLine("-- With replacement:");
40
      foreach (int x in RandomWith(list, N))
41
        Console.Write("{0} ", x);
42
      Console.WriteLine("\n-- Without replacement:");
43
      foreach (int x in RandomWithout1(copy1, N))
44
        Console.Write("{0} ", x);
45
      Console.WriteLine("\n-- Without replacement:");
46
      foreach (int x in RandomWithout2(copy2, N))
47
        Console.Write("{0} ", x);
48
      Console.WriteLine();
49
    }
50

    
51
    private static readonly C5Random rnd = new C5Random();
52

    
53
    // Select N random items from coll, with replacement.
54
    // Does not modify the given list.
55

    
56
    public static SCG.IEnumerable<T> RandomWith<T>(IIndexed<T> coll, int N) {
57
      for (int i=N; i>0; i--) { 
58
        T x = coll[rnd.Next(coll.Count)];
59
        yield return x;
60
      }
61
    }
62

    
63
    // Select N random items from list, without replacement.
64
    // Modifies the given list.
65

    
66
    public static SCG.IEnumerable<T> RandomWithout1<T>(IList<T> list, int N) {
67
      list.Shuffle(rnd);     
68
      foreach (T x in list.View(0, N)) 
69
        yield return x;
70
    }
71

    
72
    // Select N random items from list, without replacement.
73
    // Faster when list is efficiently indexable and modifiable.
74
    // Modifies the given list.
75

    
76
    public static SCG.IEnumerable<T> RandomWithout2<T>(ArrayList<T> list, int N) {
77
      for (int i=N; i>0; i--) { 
78
        int j = rnd.Next(list.Count);
79
        T x = list[j], replacement = list.RemoveLast();
80
        if (j < list.Count) 
81
          list[j] = replacement;
82
        yield return x;
83
      }
84
    }
85
  }
86
}