Solved

Using random

Posted on 2003-11-06
7
648 Views
Last Modified: 2010-04-16
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
Comment
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
  • Learn & ask questions
7 Comments
 
LVL 101

Accepted Solution

by:
mlmcc earned 50 total points
ID: 9695692
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
 
LVL 6

Expert Comment

by:MannSoft
ID: 9703140
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
 
LVL 101

Expert Comment

by:mlmcc
ID: 9705758
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
On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

 
LVL 1

Expert Comment

by:windharp
ID: 9706469
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
 
LVL 5

Expert Comment

by:scrapdog
ID: 9708311
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
 

Author Comment

by:Grandstream
ID: 9727148
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
 
LVL 101

Expert Comment

by:mlmcc
ID: 9727210
Glad I could help.. If you had posted some of the code, we probably would have solved it immediately.

mlmcc
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
This article is in regards to the Cisco QSFP-4SFP10G-CU1M cables, which are designed to uplink/downlink 40GB ports to 10GB SFP ports. I recently experienced this and found very little configuration documentation on how these are supposed to be confi…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question