Solved

# Using random 2

Posted on 2003-11-12
252 Views
program Generator;
uses crt;
cha,critical,att,strrat,defrats,defrat,defdog,defdogs,strdog,strth,defth,defths:integer;
Chance:boolean;

Okay, This time i have made a procedure that simulates a fight with you and a rat. Everything works fine until the boolean critical, Because in every moment where you use critical it uses a new number, so the attack can BOTH success and FAIL at the same time.. is there a way to choose a random number, and then keep it in a variable? , Im very grateful for answers

Procedure ratfight;
begin
clrscr;
strrat:=3;
defrats:=35;
textcolor(15);
writeln('Fight with rat started');
write('Rat has ');
textcolor(2);
write(defrats);
textcolor(15);
writeln(' Healthpoints');
defrat:=defrats;
repeat
textcolor(7);
writeln(' ');
writeln('1. Fight');
writeln('2. Furious Attack');
writeln('3. Flee');
textcolor(15);

If att=1 then begin
defrat:= defrat - random(str);
write('The rat has ');
textcolor(2);
write(defrat);
textcolor(15);
writeln(' Healthpoints left');
If defrat=0 then
writeln('You striked him first');
end;

If att=2 then begin
critical:=random(11);
If critical<6 then
chance:= true
if Critical>5 then
chance:=false

If chance=false then begin
writeln('Hit: ');
textcolor(2);
writeln('SUCESS!');
defrat:= defrat - random(str)*2;
write('The rat has ');
textcolor(2);
write(defrat);
textcolor(15);
writeln(' Healthpoints left');
cha:=1;
end;

if chance=false then begin
writeln('Hit: ');
textcolor(2);
writeln('FAILED!');
write('The rat has ');
textcolor(2);
write(defrat);
textcolor(15);
writeln(' Healthpoints left');
end;

hp:=hp-random(strrat);
write('The rat attacks you, you have ');
textcolor(8);
write(hp);
textcolor(15);
writeln(' Healthpoints left');
until (defrat=0) or (defrat<0) or (hp=0) or (hp<0);

if hp>0 then begin
writeln('You have won, congratulations!');
write('You have earned: ');
textcolor(14);
write('50');
textcolor(15);
write(' gold and ');
textcolor(5);
write('26 exp');
textcolor(15);
exp:=exp+26;
a:=a+50;

0
Question by:Grandstream
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2
• 2

LVL 100

Accepted Solution

mlmcc earned 53 total points
ID: 9737050
Just like you did here

critical:=random(11);

WHere do you need this?

mlmcc
0

Author Comment

ID: 9739482
If critical is between 1-5 , the chance becomes true, if it is between 6 and 10, the chance becomes false... in simple: if the random is 3, the attack misses,
if the random is 6, the attack hits, but.. something in that code messes it up, so the attack BOTH misses and hits, and I can't see any other solution that the random picks different numbers for both success and failed
0

LVL 11

Expert Comment

ID: 9739768
Both if statements test if critical = false. Thus they both succeed or they both fail.

It would be easier to read if you just used critical rather than critical = somevalue. Even better, as there are only two outcomes, why use two if statements: use else.

if (critical) then
{ do critical hit stuff }
else
{ do not critical hit stuff }

Hope this helps, -bcl
0

Author Comment

ID: 9740076
I Had tried that before too, but it atleast helped me out this time, thx!, because what i did was:
example:

If x:=3 then
write('CRITICAL SUCCESSFUL);
else
write('NOT SUCCESSFUL);

It said that else was wrong.. so i dumped that idea... and then i remembered, when -bcl wrote that to me, that you MUSTNT use ; at the end of the write('CRITICAL SUCCESSFUL);-line..
so now it works, tankyou!, just tell me if you want the program .. its nearly finnished.. if youre into RPG that is =)
0

LVL 11

Assisted Solution

ID: 9740667
If x:=3 then
write('CRITICAL SUCCESSFUL);
else
write('NOT SUCCESSFUL);

Note that this has two major problems: ':=' is an assignment, NOT a comparison operator. In Pascal use '=' (one equal sign) operator for comparison. Different in other languages; the other problem as you noted is the ; before else.

Another note on your code: You should indent it to show what portions of the code are controlled by the various control structures (if/while/for/procedure). Makes the code much easier to read.

-bcl
0

LVL 100

Expert Comment

ID: 10189565
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

recommend - split bcladd & mlmcc

mlmcc
Pascal Page Editor
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After hours on line I found a solution which pointed to the inherited Active Directory permissions . You have to give/allow permissions to the "Exchange trusted subsystem" for the user in the Active Directory...
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Simple Linear Regression
###### Suggested Courses
Course of the Month4 days, 12 hours left to enroll