Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Random numbers  (Read 4193 times)

0 Members and 1 Guest are viewing this topic.

Raggie62

    Topic Starter


    Greenhorn

    • Experience: Familiar
    • OS: Windows 8
    Random numbers
    « on: October 11, 2013, 05:54:28 PM »
    Hi friends. I'm slowly coming to grips with notepad++
    in a game im doing I want to get a random number between 1 and 100
    so I use
    set /a num=%random% * 100 + 1
     I then want to use that number to goto various parts of the prog
    echo number is %num%
    01 - 20  goto part a
    21 - 70 goto part b
    71 - 95 goto part c
    96 - 100 goto part d

    01 - 20 I use if %num% LSS 21 goto part a

    96 - 100 I use If %num% GTR 95 goto part d

    how do I set the middle two (21 - 70 and 71 - 95)

    Look forward to a reply

    regards, Raggie










    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: Random numbers
    « Reply #1 on: October 11, 2013, 07:51:32 PM »
    Code: [Select]
    if %num% gtr 20 if %num% lss 71 goto :partb

    Lemonilla



      Apprentice

    • "Too sweet"
    • Thanked: 70
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    Re: Random numbers
    « Reply #2 on: October 11, 2013, 07:55:34 PM »
    Code: [Select]
    set /a num=%random%*100/32768+1
    set flag=0

    if %num% LSS 20 set flag=1 && goto :a
    if %num% LSS 70 if not "%flag%"=="1" set flag=1 && goto :b
    if %num% LSS 95 if not "%flag%"=="1" set flag=1 && goto :c
    if not "%flag%"=="1" goto :d
    echo ERROR at random number 1-100 generation

    you may not need %flag% at all, but it's never a bad idea to have fail safes.
    Quote from: patio
    God Bless the DOS Helpers...
    Quote
    If it compiles, send the files.

    Raggie62

      Topic Starter


      Greenhorn

      • Experience: Familiar
      • OS: Windows 8
      Re: Random numbers
      « Reply #3 on: October 12, 2013, 01:59:02 AM »
      Thanks guys I will try these ASAP.

      Salmon Trout

      • Guest
      Re: Random numbers
      « Reply #4 on: October 12, 2013, 03:09:07 AM »
      you may not need %flag% at all, but it's never a bad idea to have fail safes.

      This is what I believe is called "cargo cult programming"

      http://en.wikipedia.org/wiki/Cargo_cult_programming


      If X is Y {
          If X is really Y {
              $Do_Something
          }
      }



      kyle_engineer



        Intermediate
      • 010010110101100
      • Thanked: 4
        • Yes
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 7
      Re: Random numbers
      « Reply #5 on: October 15, 2013, 05:30:34 PM »
      correct me if I'm wrong, but since batch is linear in its processing, you don't need anything to do that, you just need to sequence the IF statements correctly.

      e.g.
      Code: [Select]
      REM set num
      set /a num=%random%*100/32768+1

      REM argue num value
      if '%num%' LSS '21' goto result
      if '%num%' LSS '70' goto result
      if '%num%' LSS '96' goto result
      if '%num%' LSS 101 goto result
      goto error

      REM check result
      :result
      echo your random number is: %num%
      pause > nul

      REM just in case
      :error
      cls
      echo there has been an error.

      Since each IF is arguing "less than (as exact value)" and it has a GOTO as the result, so it will GOTO result if in range, otherwise go on to the next argument. If a value passes through <21, then it's bound to hit one of the following segments...

      Anyway... not sure if that logic made sense... lol
      "Any answer is only as good as it satisfies the question." - Me

      0000000100101011000 -
      010010010010000001001100010011110100110 001000000010110010100111101010101

      foxidrive



        Specialist
      • Thanked: 268
      • Experience: Experienced
      • OS: Windows 8
      Re: Random numbers
      « Reply #6 on: October 16, 2013, 07:03:38 AM »
      In essence you're right - but you've forced an ascii compare in your code, and got a number out by one. :)

      Based on your code, this kind of thing will work:

      Code: [Select]
      if %num% LSS 21 goto resultA
      if %num% LSS 71 goto resultB
      if %num% LSS 96 goto resultC
      if %num% LSS 101 goto resultD

      kyle_engineer



        Intermediate
      • 010010110101100
      • Thanked: 4
        • Yes
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 7
      Re: Random numbers
      « Reply #7 on: October 16, 2013, 10:32:00 AM »
      In essence you're right - but you've forced an ascii compare in your code, and got a number out by one. :)

      Based on your code, this kind of thing will work:

      Code: [Select]
      if %num% LSS 21 goto resultA
      if %num% LSS 71 goto resultB
      if %num% LSS 96 goto resultC
      if %num% LSS 101 goto resultD

      Yeah, I just kinda spat that out as an example. :) but since the number will only progress to the next IF argument if it exceeds the current range, then there's not really much need to scale floors and ceilings by nesting the statements - it's just letting DOS do it's thing. LOL!

      Anyway, it's always nice to make the script less verbose if possible.
      "Any answer is only as good as it satisfies the question." - Me

      0000000100101011000 -
      010010010010000001001100010011110100110 001000000010110010100111101010101