SSD 1331 Display error.

I just started using the oled display with arduino (SSD 1331). To begin with i tried using it with an ultrasonic sensor. The output is then displayed on the oled. The output which i received on the display was not what i desired. What could be the possible errors?

CODE:
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1331.h>
#include <SPI.h>
Adafruit_SSD1331 display = Adafruit_SSD1331(cs, dc, rst);

const int trigPin = 1;
const int echoPin = 2;
// defining variables
long duration;
int distance;

// You can use any (4 or) 5 pins
#define sclk 13
#define mosi 11
#define cs 10
#define rst 9
#define dc 8

// Color definitions
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600);

}
void loop() {
display.begin();
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
display.print(distance);
}

Try putting the display.begin() into setup(), and also post a better photo - I can't see what is on the screen.

CtrlAltElite:
Try putting the display.begin() into setup(), and also post a better photo - I can't see what is on the screen.

Thank you for your response. I placed display.begin() into setup() but there wasn't much difference in the display. I have also attached a photo of the display.
It is just random lines flashing in an absurd pattern.

Can you try GitHub - lexus2k/ssd1306: Driver for SSD1306, SSD1331, SSD1351, IL9163, ILI9341, ST7735, PCD8544, Nokia 5110 displays running on Arduino/ESP32/Linux (Rasperry) platforms library? It supports ssd1331 rbg oled. There is ssd1331_demo sketch in examples folder.