0

why this code doesn’t show a random number , but with different digits??? ( run it till u see same digits in one random number)

https://code.sololearn.com/cX4m50nn9VZz/?ref=app

24th May 2019, 6:26 AM
SaraAdib
SaraAdib - avatar
19 ответов
+ 2
By random two numbers after another can be the same. That is random. Try rolling the dice. At some point you will get the same number again.
24th May 2019, 7:18 AM
Dragonxiv
Dragonxiv - avatar
+ 2
You could save the last number and compare it with the new random number. If they are the same get a new random number.
24th May 2019, 7:29 AM
Dragonxiv
Dragonxiv - avatar
+ 1
You are right, we must put "a=0" at the start of the main loop, not outside; else, when some replacement is done, "a" will never go back to 0 and infinite loops are executed. So put at line 20: a=0; printf("%d %d %d %d\n", RemainerOfRandom2[0],RemainerOfRandom[1], RemainerOfRandom[2],RemainerOfRandom[3]); and you will see how many loops are performed and when some digits are replaced.
24th May 2019, 7:42 PM
Bilbo Baggins
Bilbo Baggins - avatar
0
Saraadib I'm seeing random number as output they comes randomly no unique value is coming as you mentioned
24th May 2019, 6:36 AM
Abhi
0
Abhi run it till u see it ! it will be showed finally ! :(
24th May 2019, 6:39 AM
SaraAdib
SaraAdib - avatar
0
I want to never see random number with same digits
24th May 2019, 6:42 AM
SaraAdib
SaraAdib - avatar
0
Dragonxiv what should I do to fix it?
24th May 2019, 7:26 AM
SaraAdib
SaraAdib - avatar
0
Dragonxiv this code will develope for guessing number game , and for that aim , it must produce random number with totaly different digits!
24th May 2019, 7:27 AM
SaraAdib
SaraAdib - avatar
0
In your code there are 3 bugs: BUG 1 -> internal loop at line 23: for(j=i+1;j<3;j++) { replace 3 with 4, else last digit is never checked for equality with the previous 3 digits BUG 2 -> line 34: else if(f>=5) replace with else if(f<=5), else if f<=5 it will be never replaced BUG 3: after replacing f you must check again the 4 digits for equality; a not elegant way of doing this is adding a goto to line 21 soon after having replaced f; another way is introducing an outer loop "while (need_further_check)" before line 21, and set need_further_check everytime you replace f, where need_further_check is a bool
24th May 2019, 7:37 AM
Bilbo Baggins
Bilbo Baggins - avatar
0
Bilbo Baggins really thank u , I tried to do whatever u said , can u check it again? I have a runtime error !
24th May 2019, 8:47 AM
SaraAdib
SaraAdib - avatar
0
Unfortunately you made again two bugs. BUG 1 - you are defining a variable "bool a =..." which is not seen by the outer while: the outer while sees the "int a" defined at the top of the program. So please remove the two "bool" BUG 2 - you are inverting the assignment: replace a=0 with a=1 (which means loop again) and replace a=1 with a=0 (which means stop looping). But also in this way we could have some cases in which "else a=0" hides a previous a=1, leaving sporadic cases in which there are two equal digits. So the best solution is: remove the "else a=0" to avoid hiding set a=0; before the loop use a do while instead a while in order to exec the loop at least once: a = 0; do { ....; a=1 if some replacement is done, in order to recheck; .... } while (a); Happy coding!
24th May 2019, 9:28 AM
Bilbo Baggins
Bilbo Baggins - avatar
0
Bilbo Baggins thanks for your answer , but I have an error , I think I did everything that u said right :(
24th May 2019, 11:10 AM
SaraAdib
SaraAdib - avatar
0
line 38: if you have done a replacement, you want to check again; so a must be put to 1, not to 0
24th May 2019, 11:24 AM
Bilbo Baggins
Bilbo Baggins - avatar
0
Bilbo Baggins but I have error when I put 1 see the code
24th May 2019, 7:17 PM
SaraAdib
SaraAdib - avatar
0
Bilbo Baggins now it works well😂😻💜really thank u😻😻😻
25th May 2019, 5:42 AM
SaraAdib
SaraAdib - avatar
0
You welcome! There is yet another little issue, if you really want to emulate a die. You should reenter the main loop even if some digit is equal to 0 or 7 or 8 or 9 :-),
25th May 2019, 6:31 AM
Bilbo Baggins
Bilbo Baggins - avatar
0
Bilbo Baggins I can‘t get it :( what do u mean?
25th May 2019, 8:23 AM
SaraAdib
SaraAdib - avatar
0
The four numbers must simulate dices or not? If yes you have to discard all the numbers outside the range 1-6
25th May 2019, 10:24 AM
Bilbo Baggins
Bilbo Baggins - avatar
0
Bilbo Baggins no I just want a 4 digit number
27th May 2019, 4:16 PM
SaraAdib
SaraAdib - avatar