lundi 24 juillet 2023

how does the rand() function work in c++?

im trying to get back into coding so i figured id do a magic 8 ball type of program. im having trouble with rand() as i dont understand how to set a limit on it, for example i want to have 8 answers but wouldnt using rand() make it so it picks a number from 0-32767, is there a way to set it to a desired value or is there a better way to do this?

i tried this but it didnt work, apologies if thats a simple mistake but i decided to try using what i vaguely remember, ill go with tutorials to remember later

#include <iostream>
#include <cstdlib>

RAND_MAX = 6;
int number ;

int main(){
 
while(true){
    std::cout << "ask a question: ";
     rand() << number;
     std::cout << "your number is: " << number;
     return 0;
}


}

in this specific code i tried to just set the random numbers limit to see if that worked before i continued also this was the error it gave me

Untitled-1.cpp: In function 'int main()': Untitled-1.cpp:6:13: error: lvalue required as left operand of assignment RAND_MAX = 6; ^




Aucun commentaire:

Enregistrer un commentaire