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)
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.
+ 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.
+ 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.
0
Saraadib I'm seeing random number as output they comes randomly no unique value is coming as you mentioned
0
Abhi run it till u see it ! it will be showed finally ! :(
0
I want to never see random number with same digits
0
Dragonxiv what should I do to fix it?
0
Dragonxiv this code will develope for guessing number game , and for that aim , it must produce random number with totaly different digits!
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
0
Bilbo Baggins really thank u , I tried to do whatever u said , can u check it again? I have a runtime error !
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!
0
Bilbo Baggins thanks for your answer , but I have an error , I think I did everything that u said right :(
0
line 38: if you have done a replacement, you want to check again; so a must be put to 1, not to 0
0
Bilbo Baggins but I have error when I put 1
see the code
0
Bilbo Baggins now it works well😂😻💜really thank u😻😻😻
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 :-),
0
Bilbo Baggins I can‘t get it :( what do u mean?
0
The four numbers must simulate dices or not?
If yes you have to discard all the numbers outside the range 1-6
0
Bilbo Baggins no I just want a 4 digit number