+ 2

C zero rand error

In my code I use const randomBit = rand() % 2; but then it get value 0 it show message: No output, when value is 1 it compleye code amd output. This value used only in if() condition. https://code.sololearn.com/c2370vnO4KPk/?ref=app

31st Dec 2018, 7:14 AM
Vladimir Semenov
Vladimir Semenov - avatar
12 Réponses
+ 5
Vladimir, I have tested the code, setting the <randomBit> to zero, and 123, 25, 89 for <Var>, and I did get outputs, although the numbers in outputs doesn't seem to make any sense to me ... What error message you get BTW? here I didn't get error message ... Some input and expected outputs probably ...
1st Jan 2019, 9:25 AM
Ipang
+ 4
Hi Vladimir, I don't really understand how you expect the output to be with <randomBit> being zero, but maybe try these changes: Line 8: Add type after const (compiler warning, defaulted -> int) const int randomBit = rand() % 2; Line 26: Simply increment <countf> by one countf += 1; A side note, why we have <count> and <countf>? can't we just use <count> for showing percentage? I'm a little confused here : )
1st Jan 2019, 4:12 AM
Ipang
+ 4
Vladimir, apologies, then my answer was incorrect : ) I'll stand by to see if others can come with better solution : ) (Edit) It is possible that you get that "No output" message simply because of network traffic bottleneck on SoloLearn server, it comes quite often lately, your code has no problem.
1st Jan 2019, 8:26 AM
Ipang
+ 4
Vladimir, give me the numbers you used for testing, particularly the numbers that trigger errors, I'll see if there is something I can do ...
1st Jan 2019, 8:53 AM
Ipang
+ 3
Your code has no problem, even when the input is 0. When it shows "No output", actually it is Sololearn server problem. Try click Run again
31st Dec 2018, 11:25 AM
Gordon
Gordon - avatar
+ 2
I try many times. On zero value shows error, on one value it is work fine.
31st Dec 2018, 10:39 PM
Vladimir Semenov
Vladimir Semenov - avatar
+ 2
It shows two types: randomBit equal 0: output is how many percentage in one value, for example 1.125% = 1 Value. randomBit equal 1: output is how many value in one percentage, for example 1.000% = 0.889 Value. For this reason countf may take two parameters as int for int percentage increment and as float for int value increment, but in last case percentage increment is float value. Simply I need random generator for two output variants, not for manual input.
1st Jan 2019, 8:21 AM
Vladimir Semenov
Vladimir Semenov - avatar
+ 2
But it works fine then I simply type 0 or 1 in function call: RecursvFcnTogetPrcntge(0); Error also show when I try printf generated value, 1 show and 0 shpw error.
1st Jan 2019, 8:49 AM
Vladimir Semenov
Vladimir Semenov - avatar
+ 2
Any number, I use 123 and 25 and 89, then generated value 1 it works, then zero show error.
1st Jan 2019, 9:13 AM
Vladimir Semenov
Vladimir Semenov - avatar
+ 2
Input: 89 Error then generated value 0 on russian: Нет вывода. On english: No output. Then generated value 1 it works and show output: 1.000% = 0.89 Value
1st Jan 2019, 9:52 AM
Vladimir Semenov
Vladimir Semenov - avatar
+ 2
If set randomBit directly to zero it works, if use rand() % 2 it shows error on value different from one, on one works fine. const int randomBit = 0; So it works. But if use rand it wont. const int randomBit = rand() % 2;
1st Jan 2019, 10:12 AM
Vladimir Semenov
Vladimir Semenov - avatar
+ 2
Thanks Gordie, it help and works now!!!
1st Jan 2019, 10:57 AM
Vladimir Semenov
Vladimir Semenov - avatar