Advertisement
Guest User

cs2576

a guest
Jul 24th, 2012
111
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Module Module1
  2.     Const K As Integer = 50
  3.  
  4.     Private Count As Integer
  5.  
  6.     Function RandK() As Integer
  7.         Static r As New Random
  8.         Count += 1
  9.         Return r.Next(1, K + 1)
  10.     End Function
  11.  
  12.     Private Function Rand2K1() As Integer
  13.         Return RandK() * 2 - If(RandK() > K / 2, 1, 0)
  14.     End Function
  15.  
  16.     Function Rand2Klist() As IEnumerable(Of Integer)
  17.         Count = 0
  18.         Dim lst = Enumerable.Range(1, 2 * K).ToList
  19.         Dim result As New List(Of Integer)
  20.  
  21.         Dim j = Rand2K1() - 1
  22.  
  23.         For i = 0 To 2 * K - 1
  24.             Debug.Assert(lst.Count = (2 * K - i))
  25.             Debug.Assert(result.Count = i)
  26.             Dim l = (j + RandK() + RandK() - 1) Mod (2 * K - i)
  27.             result.Add(lst(l))
  28.             lst.RemoveAt(l)
  29.             j += RandK()
  30.         Next
  31.         Debug.Assert(lst.Count = 0)
  32.         Return result
  33.     End Function
  34.  
  35.     Private sum(0 To 2 * K - 1), pos(0 To 2 * K - 1) As Integer
  36.  
  37.     Sub CheckAndStats(ByVal lst As IEnumerable(Of Integer))
  38.         Dim chkLst = lst.ToArray
  39.         Array.Sort(chkLst)
  40.         Debug.Assert(chkLst.Length = 2 * K)
  41.         Debug.Assert(chkLst(0) = 1)
  42.         Debug.Assert(chkLst(2 * K - 1) = 2 * K)
  43.  
  44.         chkLst = lst.ToArray
  45.         For i = 0 To 2 * K - 1
  46.             pos(chkLst(i) - 1) += i
  47.             sum(i) += chkLst(i)
  48.         Next
  49.     End Sub
  50.  
  51.     Sub PrintStats()
  52.         Console.WriteLine(" {0,4} {1,7} {2,7}", "no.", "Pos", "Avg")
  53.         For i = 0 To 2 * K - 1
  54.             Console.WriteLine(" {0,4} {1,7:f3} {2,7:f3}", i + 1, pos(i) / M - K, sum(i) / M - K)
  55.         Next
  56.     End Sub
  57.  
  58.     Const M As Integer = 10000
  59.  
  60.     Sub Main()
  61.         Dim someLists(0 To M) As IEnumerable(Of Integer)
  62.         For i = 0 To M
  63.             someLists(i) = Rand2Klist.ToList
  64.             Debug.Assert(Count = 6 * K + 2)
  65.         Next
  66.         For Each l In someLists
  67.             CheckAndStats(l)
  68.         Next
  69.         PrintStats()
  70.         If Debugger.IsAttached Then _
  71.             Console.ReadLine()
  72.     End Sub
  73.  
  74. End Module
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement