Link to home
Start Free TrialLog in
Avatar of Grandstream
Grandstream

asked on

Using random

Okay, I'm doing a small project for school, and as i am a huge fan of RPG games, I want to make a charactergenerator that randomizes different profession values:

randomize;
str:=random(20)+12
def:=random(15)+5;
agi:=random(7)+3;
mag:=random(1)+2;
end;

Okay, above is a warrior, his main statuspoints will go to strength and defence, i need to know a little more about the randomcommand, because the strenght here sometimes jumps down to 3...  or agi jumps up to 16. I want it to be that it randomizes a number between 20 and then adds 12 (for strength. How is that done? , I appreciate answers
ASKER CERTIFIED SOLUTION
Avatar of Mike McCracken
Mike McCracken

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MannSoft
MannSoft

Are you sure you don't reassign the values later?  When you supply a parameter to Random (as you are doing), it will return an integer value X where 0 <= X < Parameter.  So in your case:

str:=random(20)+12;
Minimum value of Random(20) is 0, add 12, minimum value of str is 12.

agi:=random(7)+3;
Maximum value of Random(7) is 6, add 3, maximum value of agi is 9.

So, unless you have run into a bug in the compiler you use, or reasign the values at another place, it's just not possible for str to be 3 and agi to be 16.

But you can always try mlmcc's solution.  When you call Random without any parameters, it returns a floating point number between 0 (inclusive) and 1 (exclusive), so his code should have the same end result.  If his code does work, then it must be a compiler bug.  If it yields the same results, then I have to go back to asking if you reassign the values somewhere else :-)
If you could show more of the code since the code is obviously in a subroutine.  Maybe the values aren't shared correctly with the rest of the program.

mlmcc
I would assume it is a conversion problem. Make sure you use the same kind of variable all over.
Random(20) results in a Word (an integer type) so you should not use variables of the "Real" kind to store it anywhere.

A second guess might be an overwriting issue. Make sure, "range checking" is turned on in your program, so the program checks if you maybe write something to a different variable that does not fit there.
Note:  make sure you only call randomize ONE TIME in the program...don't call it every time you need a set of random numbers.  I would suggest that call randomize in the first line in your main program block.
Avatar of Grandstream

ASKER

Well, You guys helped me, thanks =) , what i had done was: If the profession to be chosen was Swordsman, then:

If( y=1) then           (*Y= the profession you choose*)
randomize;
str:=random(20)+12
def:=random(15)+5;
agi:=random(7)+3;
mag:=random(1)+2;

If( y=2) then           (*Y= the profession you choose*)
randomize;
str:=random(20)+12
def:=random(15)+5;
agi:=random(7)+3;
mag:=random(1)+2;

So, i remade it like this:

If( y=1) then begin           (*Y= the profession you choose*)
randomize;
str:=random(20)+12
def:=random(15)+5;
agi:=random(7)+3;
mag:=random(1)+2;
end;

If( y=2) then begin           (*Y= the profession you choose*)
randomize;
str:=random(20)+12
def:=random(15)+5;
agi:=random(7)+3;
mag:=random(1)+2;
end;


Sorry for not sharing more of the program code.. but what the program did from the beginning was that even if i had ID , it jumped to y=2, y=3 and so on.. but Begin end; helped, thx guys
Glad I could help.. If you had posted some of the code, we probably would have solved it immediately.

mlmcc