Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 653
  • Last Modified:

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
0
Grandstream
Asked:
Grandstream
1 Solution
 
mlmccCommented:
I answered on like this elsewhere and was told
Random(20) generates a random number between 0 and 20.

I usually do my own as

str := Random() *20 + 12;

General formula
Random * range + offset

mlmcc
0
 
MannSoftCommented:
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 :-)
0
 
mlmccCommented:
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
0
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 
windharpCommented:
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.
0
 
scrapdogCommented:
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.
0
 
GrandstreamAuthor Commented:
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
0
 
mlmccCommented:
Glad I could help.. If you had posted some of the code, we probably would have solved it immediately.

mlmcc
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now