|
Answer» Hi there, another newbie here. I'm trying to make a batch file rpg using notepad. My game needs combat so I need to simulate 2D6 being rolled and adding any modifier. The results need to be as random as possible each time its run.
Any help will be gratefully received, feel free to email me at [removed to prevent spam]
Let the game begin.
Ron.To do the random number, you just use the %random% variable which will generate a number between 0 and 32768. Since you want it in the range of 1-6, you'd do something like this:
Code: [Select]Set /a DieA = %random% * 6 / 32768 + 1 Set /a DieB = %random% * 6 / 32768 + 1 Echo %DieA% - %DieB%
Personally I've found this to not be too random, but it's the best I've found so FAR ANYWAY. I used this in a Russian Rulette game I made, so I know it works.
Also, I would recommend using Notepad++ for your scripting rather than Windos' Notepad.exe. Notepad++ has SYNTAX highlighting for BAT so it makes it a lot easier to keep track of your script as you're writing it.
As a LAS note, you may consider moving this thread to the programming board as you'll probably get the best advice there.If you are going with the cmd internal random system, then you can either round off your number manually, using Code: [Select]set /a num=%random%*(upper-lower+1)/32768+loweror using modulo, which does the same thing, but more readable, and without relying on the size of %random%'s possible input Code: [Select]set /a num=%random% %% (upper-lower+1) + lower if you want to make the random number yourself then there are a bunch of methods you can use, the first is with a linear function: Code: [Select]newseed=(multiplier*oldseed+constant) %% moduluswhich is fairly effective, in fact I think that that is how the ms-dos random works anyway, although this method is usually done with really big numbers using hardware, so that it is efficient yet unpredictable. modulus represents the range of your numbers, so if you were to remake the %random% system, the modulus would be 32767, but note that this method can and will output a zero. you would be best off dealing with a large modulus and using the %random% method with the output, this probably means that %random% would be a whole lot simpler.
Another simple and effective random system is the middlesquare method, but its not really possible to use without planning its sequence of outputs. In this method you just take your previous number, square it, and take out the middle digits that would reduce the numbers size to match the seed. eg. seed: 12345 12345 squared: 152399025 the middle 5 digits of 152399025: 23990 new seed: 23990
and so on and so forth, but the problem is that some sets are just ineffective, for example, 2500 points to an output of 2500, and therefore any other number that will eventually give you 2500 will loop forever, you can increase the output by one if it would otherwise equal the original, and repeat until you FIND a unique number, but then there will be loops of numbers that are too short to use, so overall this method requires a lot of handling to use in batch, but I did make a script that middlesquares numbers for you, so it is possible.
Have fun, and good luck!
|