Exemplo n.º 1
0
// calculateBitStatistics performs about the simplest DieHard randomness test --
// checks the bit count statistics of random numbers to make sure they match
// a binomial distribution.
// Returns the sum-squared-difference metric of the observed distribution and the
// binomial distribution, normalized to the number of trials.
func calculateBitStatistics(mt *MersenneTwister, trials int) float64 {
	var bitsSet [33]int64
	for i := 0; i < trials; i++ {
		bitsSet[countBits(mt.Uint32())]++
	}

	// bitsSet should follow a binomial distribution pretty closely
	var ssd int64
	sum := 4294967296 // Sum of the binomial distribution from 0-32.
	for i := 0; i < 33; i++ {
		z := big.Int{}
		binomial := int64(z.Binomial(32, int64(i)).Uint64()) * int64(trials) / int64(sum)
		ssd += (bitsSet[i] - binomial) * (bitsSet[i] - binomial)
	}
	return math.Sqrt(float64(ssd)) / float64(trials)
}
Exemplo n.º 2
0
func main() {
	N := 20

	// drawing it out, I found that it
	// comes down to pascal's triangle,
	// which simplifies to N choose R:
	// N! / (R! * (N - R)!)
	// where R = N / 2
	// so:
	// N! / ((N/2)! * (N/2)!)

	// So the solution is easy:
	a := new(big.Int)

	fmt.Println(a.Binomial((int64)(N*2), (int64)(N)))
}
Exemplo n.º 3
0
func TestComb(test *testing.T) {
	nums := []int32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	expTotal := new(big.Int)
	expTotal.Binomial(int64(len(nums)), 5)

	actTotal := new(big.Int)
	c := Comb(nums, 5)
	loop := true
	vals := make([]int32, 5)
	for loop {
		loop = c(vals)
		actTotal.Add(actTotal, big.NewInt(1))
	}
	if expTotal.Cmp(actTotal) != 0 {
		test.Fatalf("Expected %v combinations, but saw %v\n", expTotal, actTotal)
	}
}
Exemplo n.º 4
0
func mainExec() int {
	start := time.Now()
	r := new(big.Int)
	fmt.Println(r.Binomial(1000, 10))

	numCPUs := runtime.NumCPU()
	runtime.GOMAXPROCS(numCPUs)

	urls := make(chan int)
	done := make(chan struct{})
	c := make(chan result)

	var wg sync.WaitGroup
	const numWorkers = 100
	wg.Add(numWorkers)
	for i := 0; i < numWorkers; i++ {
		go func() {
			worker(done, urls, c)
			wg.Done()
		}()
	}

	go func() {
		wg.Wait()
		close(urls)
		close(c)
	}()

	urls <- -1

	count := 0
	for a := range c {
		LOG.Debug("==== %d %s", a.url, a.content)
		count++
		if count > len(HCIDS) {
			close(done)
			break
		}
	}

	elapsed := time.Since(start)
	LOG.Debug("It took %s", elapsed)
	return count - 1
}
Exemplo n.º 5
0
func TestMap(t *testing.T) {
	start := time.Now()
	r := new(big.Int)
	fmt.Println(r.Binomial(1000, 10))

	numCPUs := runtime.NumCPU()
	runtime.GOMAXPROCS(numCPUs) // 모든 CPU를 사용하도록 설정

	urls := make(chan string)   // 작업을 요청할 채널
	done := make(chan struct{}) // 작업 고루틴에 정지 신호를 보낼 채널
	c := make(chan result)      // 결괏값을 저장할 채널

	var wg sync.WaitGroup
	const numWorkers = 10
	wg.Add(numWorkers)
	for i := 0; i < numWorkers; i++ { // 작업을 처리할 고루틴을 10개 생성
		go func() {
			worker(done, urls, c)
			//			wg.Done()
		}()
	}

	//	go func() {
	//		wg.Wait() // 고루틴이 끝날 때까지 대기
	//		close(c)  // 대기가 끝나면 결괏값 채널을 닫음
	//	}()

	urls <- "https://github.com/pyrasis/following" // 최초 URL을 보냄

	count := 0
	for r := range c { // 결과 채널에 값이 들어올 때까지 대기한 뒤 값을 가져옴
		fmt.Println(r.name)

		count++
		if count > 100 { // 100명만 출력한 뒤
			close(done) // done을 닫아서 worker 고루틴을 종료
			break
		}
	}

	elapsed := time.Since(start)
	log.Printf("It took %s", elapsed)
}
Exemplo n.º 6
0
func mainExec() map[string]string {
	start := time.Now()
	r := new(big.Int)
	fmt.Println(r.Binomial(1000, 10))

	numCPUs := runtime.NumCPU()
	runtime.GOMAXPROCS(numCPUs)

	p := NewPipeline()
	p.Run()

	following := &GraphiteFetch{
		fetchedUrl: &FetchedUrl{m: make(map[int]error)},
		p:          p,
		result:     make(chan FetchedResult),
		url:        -1,
	}
	p.request <- following

	result := make(map[string]string)
	count := 0
	LOG.Debug("=len(HCIDS)=== %d", len(HCIDS))
	for a := range following.result {
		LOG.Debug("==== %d %s", a.url, a.content)
		count++
		countStr := strconv.Itoa(count)
		result[countStr] = a.content
		if count > len(HCIDS) {
			close(p.done)
			break
		}
	}

	elapsed := time.Since(start)
	LOG.Debug("It took %s", elapsed)
	return result
}
Exemplo n.º 7
0
func main() {
	var b, c big.Int
	for n := int64(0); n < 15; n++ {
		fmt.Println(c.Div(b.Binomial(n*2, n), c.SetInt64(n+1)))
	}
}
Exemplo n.º 8
0
func main() {
	var bincoeff = new(big.Int)
	bincoeff.Binomial(40, 20)
	fmt.Printf("%v\n", bincoeff)
}
Exemplo n.º 9
0
func prob15() {
	x := new(big.Int)
	x.Binomial(40, 20)
	fmt.Println(x)
}
Exemplo n.º 10
0
// PHole returns the probability of having a given class of hole cards given
// that scards have already been seen.
//
// Here are two example calculations:
//	          Me   Opp  Board  P(AA)
//	Pre-deal  ??   ??   ???    (4 choose 2) / (52 choose 2) ~= 0.0045
//	Pre-flop  AKs  ??   ???    (3 choose 2) / (50 choose 2) ~= 0.0024
func PHole(hd *HandDist, scards []string) float64 {
	holes := hd.Ints()
	// Count how many hands to eliminate from the holes class because a card in
	// that hand has already been seen.
	elim := 0
	for _, hand := range holes {
		for _, card := range hand {
			for _, seen := range cards.StoI(scards) {
				if card == seen {
					elim++
					goto nextHand
				}
			}
		}
	nextHand:
	}
	allHands := new(big.Int)
	allHands.Binomial(int64(52-len(scards)), 2)
	return float64(len(holes)-elim) / float64(allHands.Int64())
}

/*
// FIXME
// CondProbs returns the PTable for P(hole | action) given the cards that have
// currently been seen, and the probabilties P(action) and P(action | hole).
// The formula for calculating the conditional probability P(hole | action):
//
//	                   P(hole) * P(action | hole)
//	P(hole | action) = --------------------------
//	                            P(action)
//
// Weisstein, Eric W. "Conditional Probability." From MathWorld--A Wolfram Web
// Resource. http://mathworld.wolfram.com/ConditionalProbability.html
//
func CondProbs(scards []string, pActHole *PTable, pAction []float64) *PTable {
	for _, vals := range actionDist {
		NewRRSDist(actionDist[:3]...) (* (PHole cards [r1 r2 s]) prob)])]
  (apply array-map (flatten values))))
*/

type Lottery struct {
	// Maybe should use ints or fixed point to make more accurate.
	probs  []float64
	prizes []string
}

func (this *Lottery) String() string {
	b := bytes.NewBufferString("[ ")
	for i := 0; i < len(this.probs); i++ {
		fmt.Fprintf(b, "%s:%.2f ", this.prizes[i], this.probs[i])
	}
	b.WriteString("]")
	return b.String()
}

// Convert a discrete distribution (array-map {item prob}) into a lottery. The
// probabilities should add up to 1
func NewLottery(dist map[string]float64) *Lottery {
	sum := 0.0
	lotto := &Lottery{}
	for key, val := range dist {
		if val != 0 {
			sum += val
			lotto.probs = append(lotto.probs, sum)
			lotto.prizes = append(lotto.prizes, key)
		}
	}
	return lotto
}

// Draw a winner from a Lottery. If at least one value in the lottery is not >=
// 1, then the greatest value is effectively rounded up to 1.0"
func (this *Lottery) Play() string {
	draw := rand.Float64()
	for i, p := range this.probs {
		if p > draw {
			return this.prizes[i]
		}
	}
	return this.prizes[len(this.prizes)-1]
}
Exemplo n.º 11
0
func main() {
	var x, y int64 = 20, 20
	n := x + y
	paths := new(big.Int)                                  // create a new big.Int value using package math/big
	fmt.Printf("There are %d paths", paths.Binomial(n, x)) // Binomial() method sets value to the binomial coefficient of (n,k)
}
Exemplo n.º 12
0
func main() {
	result := new(big.Int)
	fmt.Println(result.Binomial(2*20, 20))
}