1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Random numbers

Discussion in 'Software Development' started by Raggy63, Sep 10, 2014.

Thread Status:
Not open for further replies.
Advertisement
  1. Raggy63

    Raggy63 Thread Starter

    Joined:
    Sep 10, 2014
    Messages:
    8
    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. Ent

    Ent Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,465
    First Name:
    Josiah
    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.
     
  3. foxidrive

    foxidrive Banned

    Joined:
    Oct 20, 2012
    Messages:
    793
    I don't use the above - but in batch code you can use this:

    Code:
    @echo off
    :loop
    set /a "dice=(%random% %% 6) +1"
    echo "%dice%"
    pause
    goto :loop
    When you use it in a for loop, you will have to read about delayed expansion to get the variable.
     
  4. Raggy63

    Raggy63 Thread Starter

    Joined:
    Sep 10, 2014
    Messages:
    8
    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
     
  5. Raggy63

    Raggy63 Thread Starter

    Joined:
    Sep 10, 2014
    Messages:
    8
    Hi Thanks for this, I will give it a go. appreciate your help
     
  6. Ent

    Ent Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,465
    First Name:
    Josiah
    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.
     
  7. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1133346

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice