Random function in quiz question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

11th Feb 2017, 11:39 AM
J.G.
J.G. - avatar
15 Answers
+ 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.
11th Feb 2017, 12:11 PM
Hatsy Rei
Hatsy Rei - avatar
+ 9
@J.G. Anything multiplied by 0 is 0, so no matter what rand()%4 is, x*=rand()%4 would be 0.
11th Feb 2017, 12:15 PM
Hatsy Rei
Hatsy Rei - avatar
+ 7
I think J.G. wouldn't have missed it if it asked for maximal num sequence.
11th Feb 2017, 12:13 PM
Hatsy Rei
Hatsy Rei - avatar
+ 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.
11th Feb 2017, 12:17 PM
J.G.
J.G. - avatar
+ 7
Yeah lol. Get back to us if it comes up again.
11th Feb 2017, 12:18 PM
Hatsy Rei
Hatsy Rei - avatar
+ 6
It's probably doing rand() % 1, which will only output 0. The rand() value would be irrelevant to final answer.
11th Feb 2017, 11:42 AM
Hatsy Rei
Hatsy Rei - avatar
+ 6
No, it used rand()%4. I know this for sure.
11th Feb 2017, 11:44 AM
J.G.
J.G. - avatar
+ 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?
11th Feb 2017, 11:50 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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)
11th Feb 2017, 11:52 AM
J.G.
J.G. - avatar
+ 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.
11th Feb 2017, 11:56 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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...🤔
11th Feb 2017, 11:58 AM
Burey
Burey - avatar
+ 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.
11th Feb 2017, 12:09 PM
J.G.
J.G. - avatar
+ 6
could be it asked what is the maximal number sequence that could be printed?
11th Feb 2017, 12:12 PM
Burey
Burey - avatar
+ 6
hmm... Interesting answers. @Hatsy Rei can you explain how you would know the answer to that?
11th Feb 2017, 12:13 PM
J.G.
J.G. - avatar
+ 6
Thanks Hatsy. Yeah, I'm sure it wasn't a maximum. It was just outputting 3 values from the rand function.
11th Feb 2017, 12:14 PM
J.G.
J.G. - avatar