Solved

Using random

Posted on 2003-11-06
7
643 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
7 Comments
 
LVL 100

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 100

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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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 100

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Extract text from webbrowser 5 1,394
Regexp replace from bracketed ascii number to the corresponding char 3 486
Secure Boot on W8 & 8.1 8 46
Mydac connection data base issue 3 158
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
February 24, 2017 — On February 23, Travis Ormandy, a vulnerability researcher at Google, reported on Twitter (https://twitter.com/taviso/status/834900838837411840) that massive stores of data have been leaked by CloudFlare, a company that provide…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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…

803 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