BASIC

Author Topic: RANDOM Numbers with negative range  (Read 1022 times)

Offline aroldo

  • Mr. Polyvector
  • ***
  • Posts: 143
    • View Profile
    • APD Consulting, Inc.
RANDOM Numbers with negative range
« on: 2013-Aug-03 »
This sounds silly but how can I usee the RND function to generate a random number between -1 and 1? Instead of 0-1?
[a http://apd-games.com/][img http://apd-games.com/images/APDGames135.png][/a]
MacBook Pro OS X El Capitan
XCode Version 7
iPhone 6 running  iOS 9
iPad Mini running  iOS 7.1
Galaxy S5
Dell Latitude Windows 8 Enterprise
Palm Pre, Palm Pre2

Offline spicypixel

  • Prof. Inline
  • *****
  • Posts: 646
  • Pixel Artist
    • View Profile
    • SpicyPixel.net
Re: RANDOM Numbers with negative range
« Reply #1 on: 2013-Aug-03 »
RND(2)-1
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4177
    • View Profile
    • Portfolio
Re: RANDOM Numbers with negative range
« Reply #2 on: 2013-Aug-04 »
Only integers or do you need float point?

Offline aroldo

  • Mr. Polyvector
  • ***
  • Posts: 143
    • View Profile
    • APD Consulting, Inc.
Re: RANDOM Numbers with negative range
« Reply #3 on: 2013-Aug-04 »
Thank you spicypixel and erico,

For float I did this:

Code: GLBasic [Select]
LOCAL r# = (RND(20)*0.1)-1 // Genereates random numbers from -0.9 to 0.9
« Last Edit: 2013-Aug-04 by aroldo »
[a http://apd-games.com/][img http://apd-games.com/images/APDGames135.png][/a]
MacBook Pro OS X El Capitan
XCode Version 7
iPhone 6 running  iOS 9
iPad Mini running  iOS 7.1
Galaxy S5
Dell Latitude Windows 8 Enterprise
Palm Pre, Palm Pre2