+ 8
Random function in quiz question
I came across, during a challenge, a question that used the random function in it. It looped through three times, each time outputting a number using the random function. Is there a way to know what it would output? Am I missing something?
15 Antworten
+ 7
One good possibility was that it could have been:
int x = 0;
for (i=0; i <=4; i+=2){
x*=rand()%4;
cout << x;
}
Else idk.
+ 9
@J.G. Anything multiplied by 0 is 0, so no matter what rand()%4 is, x*=rand()%4 would be 0.
+ 7
I think J.G. wouldn't have missed it if it asked for maximal num sequence.
+ 7
Oh... I overlooked the simple part. That could be a possibility. Normally I don't forget about those parts!😐Thanks for the answers! I'll see if it comes up again.
+ 7
Yeah lol. Get back to us if it comes up again.
+ 6
It's probably doing rand() % 1, which will only output 0. The rand() value would be irrelevant to final answer.
+ 6
No, it used rand()%4. I know this for sure.
+ 6
Was it asking for program output or asking if the loop was infinite, or was it actually asking for loop count? Was it an MCQ or a subjective?
+ 6
It was asking for the output. It had a for loop that would run three times, and it would output everything in a row (no spaces)
+ 6
Was there anything else in the expression apart from rand()%4?
... Well, we can go on all day with this. There is no way to accurately predict the output of rand(). So either the question is faulty, or it actually has a turn somewhere wherein the random value would no longer be relevant to output.
+ 6
i actually submitted some questions with rand()%something
only asked what could be the maximum inputted value tho, only requires some basic understanding of the rand() function and % operator
could have been in python tho...🤔
+ 6
Here's what I recall of the code:
int x = 0;
for (i=0; i <=4; i+=2){
x=rand()%4;
cout << x;
}
I don't remember specifically the syntax they used for the rand function. That's the best that I remember it.
+ 6
could be it asked what is the maximal number sequence that could be printed?
+ 6
hmm... Interesting answers. @Hatsy Rei can you explain how you would know the answer to that?
+ 6
Thanks Hatsy. Yeah, I'm sure it wasn't a maximum. It was just outputting 3 values from the rand function.