Solved

Using random

Posted on 2003-11-06
7
640 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Expert Comment

by:windharp
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Glad I could help.. If you had posted some of the code, we probably would have solved it immediately.

mlmcc
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Delphi TDBX too many connection problem 2 1,147
delphi 8 615
Secure Boot on W8 & 8.1 8 44
Mydac connection data base issue 3 106
Veeam Backup & Replication has added a new integration – Veeam Backup for Microsoft Office 365.  In this blog, we will discuss how you can benefit from Office 365 email backup with the Veeam’s new product and try to shed some light on the needs and …
In this article, I will show you HOW TO: Create your first Windows Virtual Machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, the Windows OS we will install is Windows Server 2016.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now