Not necessarily. Whether the stocks should be related depends on what you want the machine-learning model to learn.
If you are training one model using observations from several stocks, using stocks with some economic relationship—such as companies in the same industry—can be helpful because their price behavior may be generated by similar underlying factors. However, requiring a strong correlation between the stocks is not necessary.
In fact, using only highly correlated stocks can provide a lot of redundant information. A broader group of stocks may help the model learn patterns that generalize beyond one particular company or industry.
The more important issue is that your input variables are constructed consistently. For example, rather than feeding raw stock prices into the model, you might calculate comparable technical features for each stock, such as returns, moving-average differences, RSI, MACD, volatility, or volume changes.
You should also be careful about data leakage and overfitting. Financial time-series data should normally be divided chronologically rather than randomly, so the model is trained on past observations and tested on genuinely unseen future observations.
So there does not have to be a particular relationship between the stocks. The appropriate universe of stocks depends on whether your objective is to build a model specifically for one type of stock or a more general model that performs across many securities.