To get random bytes from a file at 23 different positions, you can use Delphi’s TFileStream
in combination with the Random
function. Here’s an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
unit Unit1; interface uses Windows, Classes, SysUtils; procedure ReadRandomBytes(const fileName: string; numPoints: Integer); implementation procedure ReadRandomBytes(const fileName: string; numPoints: Integer); var FileStream: TFileStream; FileSize: Int64; Position: Int64; Buffer: Byte; i: Integer; begin try // Open the file in read-only mode FileStream := TFileStream.Create(fileName, fmOpenRead or fmShareDenyNone); try // Get the size of the file FileSize := FileStream.Size; // Check if there are enough bytes in the file if FileSize < numPoints then begin Writeln(‘Error: File size is less than the requested number of points.’); Exit; end; // Set the random seed based on the current time Randomize; // Read a random byte at 23 different positions for i := 1 to numPoints do begin // Generate a random position within the file size Position := Random(FileSize); // Set the file stream position FileStream.Position := Position; // Read a single byte from the file FileStream.ReadBuffer(Buffer, SizeOf(Buffer)); // Process the random byte as needed Writeln(Format(‘Random Byte %d at Position %d: %d’, [i, Position, Buffer])); end; finally // Close the file stream FileStream.Free; end; except on E: Exception do Writeln(‘Error: ‘ + E.Message); end; end; end. |
You can call this procedure by providing the path to the file and the number of points:
1 2 3 |
begin ReadRandomBytes(‘C:\Path\To\Your\File.bin’, 23); end. |
This example uses Random
to generate random positions within the file and reads a single byte at each random position. Adjust the code according to your specific requirements.
Leave a Reply