File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -357,9 +357,32 @@ func modifyData(data []byte, ratio float64) []byte {
357357 copy (result , data )
358358
359359 modifyCount := int (float64 (len (data )) * ratio )
360- for i := 0 ; i < modifyCount ; i ++ {
361- pos := secureRandInt (len (result ))
362- result [pos ] = byte (secureRandInt (256 ))
360+
361+ // For small data or high ratios, ensure unique positions
362+ if modifyCount > 0 && len (data ) > 0 {
363+ if modifyCount >= len (data ) {
364+ // Modify all positions
365+ for i := range result {
366+ result [i ] = byte (secureRandInt (256 ))
367+ }
368+ } else {
369+ // Create a list of all positions and shuffle to get unique positions
370+ positions := make ([]int , len (data ))
371+ for i := range positions {
372+ positions [i ] = i
373+ }
374+
375+ // Fisher-Yates shuffle to randomize positions
376+ for i := len (positions ) - 1 ; i > 0 ; i -- {
377+ j := secureRandInt (i + 1 )
378+ positions [i ], positions [j ] = positions [j ], positions [i ]
379+ }
380+
381+ // Modify the first modifyCount positions from the shuffled list
382+ for i := 0 ; i < modifyCount ; i ++ {
383+ result [positions [i ]] = byte (secureRandInt (256 ))
384+ }
385+ }
363386 }
364387
365388 return result
You can’t perform that action at this time.
0 commit comments