Tech Support Guy banner
Status
Not open for further replies.

Random numbers

2K views 5 replies 3 participants last post by  Ent 
#1 ·
Hi, first off all, apologies if im in wrong section.

Using Notepad ++ batch code I am trying to make a simple RPG. I need to simulate rolling dice to get a random number that is different each time I run it. Sometimes it will be a six sided dice, sometimes a 4 sided or more.
Can anyone help please.
 
#2 ·
Could you post the code you have thus far?

Aside: Is there a reason it has to be done in Batch? It's not exactly the most powerful language out there, and I fear that this won't be the last time you find yourself frustrated by its limitations.
 
#4 ·
Could you post the code you have thus far?

Aside: Is there a reason it has to be done in Batch? It's not exactly the most powerful language out there, and I fear that this won't be the last time you find yourself frustrated by its limitations.
Hi thanks for response. I use Batch in Notepad simply because I've not learnt CC or anything other than that, though I used to use basic. I know Batch is limited, but my 'game' is very simple. What I have used so far is this

: dice
set /a die=%random% *6/32768+1
echo result is :%die%
pause

what happens next depends on result rolled. First time I run the sequence always gives me the same number (6 in this case) I need it to be a truly random number each time it runs.. I'm no genius so it needs to be simple

Thanks again
 
#6 ·
Foxidrive's final sentence seems important. You probably need to use delayed expansion (for either). Have a look here for a bit of an explanation as to how and why:
http://ss64.com/nt/delayedexpansion.html

(The short answer is enable it, and then replace the %s around random with !s.)

Aside: there is a tiny bias toward getting 1 or 2 from Foxidrive's code. It's imperceptible for this, maybe one time in five and a half thousand you'd throw a number that's unfairly low. The reason is that 32768 isn't a multiple of 6. That means there are two extra numbers at the end (32766 and 32767) with mod 0 and 1 which aren't balanced by four more numbers to return the other possible throws.
In the code that you posted originally, there is a similar tiny bias but this time it's toward getting a 1 or a 4.
In this, you shouldn't worry about it, but it's something to be aware of if you want to make something more serious.
 
Status
Not open for further replies.
You have insufficient privileges to reply here.
Top