Data update
This commit is contained in:
parent
72eb4943cb
commit
4d5544505c
2347 changed files with 62432 additions and 16731 deletions
121
Task/Knuths-algorithm-S/Fortran/knuths-algorithm-s.f
Normal file
121
Task/Knuths-algorithm-S/Fortran/knuths-algorithm-s.f
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
module knuth_s
|
||||
implicit none
|
||||
private
|
||||
public :: s_of_n_creator, free_sample, sample_state, s_of_n
|
||||
|
||||
! Type to hold sample state
|
||||
type :: sample_state
|
||||
integer :: n
|
||||
integer :: count
|
||||
integer, allocatable :: sample(:)
|
||||
end type sample_state
|
||||
|
||||
contains
|
||||
|
||||
! Subroutine to create the s_of_n sampling function
|
||||
subroutine s_of_n_creator(n, state)
|
||||
integer, intent(in) :: n
|
||||
type(sample_state), pointer :: state
|
||||
|
||||
allocate (state)
|
||||
state % n = n
|
||||
state % count = 0
|
||||
allocate (state % sample(n))
|
||||
end subroutine s_of_n_creator
|
||||
|
||||
! Function to perform sampling
|
||||
function s_of_n(state, item) result(current_sample)
|
||||
type(sample_state), pointer :: state
|
||||
integer, intent(in) :: item
|
||||
integer, pointer :: current_sample(:)
|
||||
real :: r
|
||||
integer :: j
|
||||
|
||||
! Add item to sample if we haven't reached n yet
|
||||
if (state % count < state % n) then
|
||||
state % count = state % count + 1
|
||||
state % sample(state % count) = item
|
||||
else
|
||||
! Knuth's Algorithm S: select with probability n/i
|
||||
call random_number(r)
|
||||
if (r < real(state % n) / real(state % count + 1)) then
|
||||
! Randomly replace one of the existing items
|
||||
call random_number(r)
|
||||
j = int(r * state % n) + 1
|
||||
state % sample(j) = item
|
||||
end if
|
||||
state % count = state % count + 1
|
||||
end if
|
||||
current_sample => state % sample
|
||||
end function s_of_n
|
||||
|
||||
! Cleanup function to deallocate sample state
|
||||
subroutine free_sample(state)
|
||||
type(sample_state), pointer :: state
|
||||
deallocate (state % sample)
|
||||
deallocate (state)
|
||||
end subroutine free_sample
|
||||
|
||||
end module knuth_s
|
||||
|
||||
program test_knuth_s
|
||||
use knuth_s
|
||||
implicit none
|
||||
type(sample_state), pointer :: state
|
||||
integer, pointer :: sample(:)
|
||||
integer :: i, j, k, reps
|
||||
integer :: frequencies(0:9)
|
||||
character(len=50) :: sample_str
|
||||
integer :: n, timex(8)
|
||||
integer, allocatable :: seed(:)
|
||||
call random_seed(size=n)
|
||||
allocate (seed(n))
|
||||
call date_and_time(values=timex)
|
||||
seed(1) = timex(5) * 3600000 + timex(6) * 60000 + timex(7) * 1000 + timex(8)
|
||||
do i = 2, n
|
||||
seed(i) = seed(1) + 37 * (i - 1)
|
||||
end do
|
||||
! Initialize random seed
|
||||
call random_seed(put=seed)
|
||||
|
||||
! Show one run's sampling progression
|
||||
print *, 'Single run samples for n = 3:'
|
||||
call s_of_n_creator(3, state)
|
||||
do i = 0, 9
|
||||
sample => s_of_n(state, i)
|
||||
! Format sample output
|
||||
sample_str = ''
|
||||
write (sample_str, '("[", *(I0, ", "))') sample(1:min(state % count, 3))
|
||||
if (state % count > 0) then
|
||||
sample_str = trim(sample_str)
|
||||
sample_str = sample_str(1:len_trim(sample_str) - 1)//']'
|
||||
else
|
||||
sample_str = '[]'
|
||||
end if
|
||||
print '(A, I0, A, A)', ' Item: ', i, ' -> sample: ', trim(sample_str)
|
||||
end do
|
||||
call free_sample(state)
|
||||
|
||||
! Initialize frequency counter
|
||||
frequencies = 0
|
||||
|
||||
! Run 100,000 repetitions to count final sample frequencies
|
||||
reps = 100000
|
||||
do i = 1, reps
|
||||
call s_of_n_creator(3, state)
|
||||
do j = 0, 9
|
||||
sample => s_of_n(state, j)
|
||||
end do
|
||||
! Count frequencies from final sample
|
||||
do k = 1, 3
|
||||
frequencies(sample(k)) = frequencies(sample(k)) + 1
|
||||
end do
|
||||
call free_sample(state)
|
||||
end do
|
||||
|
||||
! Print frequency results
|
||||
print *, 'Test item frequencies for ', reps, ' runs:'
|
||||
do i = 0, 9
|
||||
print '(I2, ": ", I5)', i, frequencies(i)
|
||||
end do
|
||||
end program test_knuth_s
|
||||
98
Task/Knuths-algorithm-S/FreeBASIC/knuths-algorithm-s.basic
Normal file
98
Task/Knuths-algorithm-S/FreeBASIC/knuths-algorithm-s.basic
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
Type SampleState
|
||||
sample(0 To 99) As Integer ' Array to store the sample (arbitrary maximum size)
|
||||
sampleSize As Integer ' Current sample size
|
||||
i As Integer ' Counter of processed elements
|
||||
n As Integer ' Desired sample size
|
||||
End Type
|
||||
|
||||
' Function to generate a random number between 0 and max-1
|
||||
Function randInt(Byval max As Integer) As Integer
|
||||
Return Int(Rnd() * max)
|
||||
End Function
|
||||
|
||||
' Function to create a new sampler
|
||||
Function createSampler(Byval n As Integer) As SampleState
|
||||
Dim sampler As SampleState
|
||||
sampler.sampleSize = 0
|
||||
sampler.i = 0
|
||||
sampler.n = n
|
||||
Return sampler
|
||||
End Function
|
||||
|
||||
' Function to process a new element and update the sample
|
||||
Function sampleOfN(Byref sampler As SampleState, Byval item As Integer) As Integer Ptr
|
||||
sampler.i += 1
|
||||
|
||||
If sampler.i <= sampler.n Then
|
||||
sampler.sample(sampler.sampleSize) = item
|
||||
sampler.sampleSize += 1
|
||||
Else
|
||||
If randInt(sampler.i) < sampler.n Then
|
||||
sampler.sample(randInt(sampler.n)) = item
|
||||
End If
|
||||
End If
|
||||
|
||||
Return @sampler.sample(0)
|
||||
End Function
|
||||
|
||||
Sub printArray(arr() As Integer, Byval size As Integer)
|
||||
Print "["
|
||||
For i As Integer = 0 To size-1
|
||||
Print arr(i)
|
||||
If i < size-1 Then Print ", "
|
||||
Next
|
||||
Print "]"
|
||||
End Sub
|
||||
|
||||
Sub main()
|
||||
Dim As Integer i, j
|
||||
|
||||
Dim As Integer frequency(0 To 9)
|
||||
For i = 0 To 9
|
||||
frequency(i) = 0
|
||||
Next
|
||||
|
||||
Print "Single run samples for n = 3:"
|
||||
|
||||
Dim As SampleState sampler = createSampler(3)
|
||||
Dim As Integer Ptr samplePtr
|
||||
Dim As Integer sample(0 To 2)
|
||||
|
||||
For i = 0 To 9
|
||||
samplePtr = sampleOfN(sampler, i)
|
||||
|
||||
For j = 0 To sampler.sampleSize-1
|
||||
sample(j) = samplePtr[j]
|
||||
Next
|
||||
|
||||
Print " Item: " & i & " -> sample: ";
|
||||
Print "[ ";
|
||||
For j = 0 To sampler.sampleSize-1
|
||||
Print Chr(8) & sample(j);
|
||||
If j < sampler.sampleSize-1 Then Print ", ";
|
||||
Next
|
||||
Print "]"
|
||||
Next
|
||||
|
||||
For i = 0 To 99999
|
||||
sampler = createSampler(3)
|
||||
|
||||
For j = 0 To 9
|
||||
samplePtr = sampleOfN(sampler, j)
|
||||
Next
|
||||
|
||||
For j = 0 To sampler.n-1
|
||||
frequency(samplePtr[j]) += 1
|
||||
Next
|
||||
Next
|
||||
|
||||
Print !"\nTest item frequencies for 100000 runs:"
|
||||
For i = 0 To 9
|
||||
Print " " & i & ": " & frequency(i)
|
||||
Next
|
||||
End Sub
|
||||
|
||||
Randomize Timer
|
||||
main()
|
||||
|
||||
Sleep
|
||||
36
Task/Knuths-algorithm-S/JavaScript/knuths-algorithm-s.js
Normal file
36
Task/Knuths-algorithm-S/JavaScript/knuths-algorithm-s.js
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
class SOfN {
|
||||
constructor(n) {
|
||||
this.m = this.n = n
|
||||
this.s = []
|
||||
}
|
||||
|
||||
add(item) {
|
||||
if (this.s.length < this.n) {
|
||||
this.s.push(item)
|
||||
} else {
|
||||
const rand = Math.floor(Math.random() * ++this.m)
|
||||
if (rand < this.n) {
|
||||
this.s[rand] = item
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function main() {
|
||||
for (const [n, m] of [[3, 3], [3, 10]]) {
|
||||
const freqs = new Array(m).fill(0)
|
||||
|
||||
for (let i = 0; i < 1e5; ++i) {
|
||||
const sOfN = new SOfN(n)
|
||||
for (let x = 0; x < m; ++x) {
|
||||
sOfN.add(x)
|
||||
}
|
||||
for (const d of sOfN.s) {
|
||||
++freqs[d]
|
||||
}
|
||||
}
|
||||
console.log(`Results for n=${n}, m=${m}: [${freqs.join(', ')}]`)
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue