I would like to set up a final exam of 15 questions. I would like to randomize 40 questions so the students will randomly get 15 of the 40 questions. When I add random questions to the quiz and preview it, an error appears.
In the quiz bank, questions are created in a category. For a course, you may have created multiple categories, each with specific questions.
This is one of my question banks with four categories.

when I created the quiz, I can only select the number of questions that exists in a category.

You need to look at your question bank for the course and see if there are multiple categories. If there are multiple categories, you can only add the number of random questions that are available in that category.
I randomize 99% my questions. With rare exception, when one specific quiz question is required, I will create a new Quiz Item for those to keep that out of the randomization pool and my own housekeeping.
If no one has attempted the quiz and you want all 40 quiz questions randomize, just delete the 15 specific questions and change the randomized question number to 40.
If no one has attempted the quiz and you want all 40 quiz questions randomize, just delete the 15 specific questions and change the randomized question number to 40.
if the quiz is only 15 questions, you select add, random, 15. Done.
You do not need to account for the balance of the 40 questions when you create the quiz.
The system will select 15 questions out of the 40 for each quiz attempt.
If it doesn't work after that, please consider a screen capture of your quiz category and quiz (question) build just like I included above.
You do not need to account for the balance of the 40 questions when you create the quiz.
The system will select 15 questions out of the 40 for each quiz attempt.
If it doesn't work after that, please consider a screen capture of your quiz category and quiz (question) build just like I included above.