BlitzMax

BlitzMax

  • Downloads
  • Docs
  • API
  • Resources
  • About

›Random

BlitzMax API

  • Introduction

BRL

    Audio

    • Introduction
    • TChannel
    • TSound

    AudioSample

    • Introduction
    • TAudioSample
    • TAudioSampleLoader

    Bank

    • Introduction
    • TBank

    BankStream

    • Introduction
    • TBankStream

    Base64

    • Introduction
    • TBase64

    Blitz

    • Introduction
    • TArrayBoundsException
    • TBlitzException
    • TNullFunctionException
    • TNullMethodException
    • TNullObjectException
    • TOutOfDataException
    • TRuntimeException
  • BRL.BMPLoader
  • Clipboard

    • Introduction
    • TClipboard
    • TClipboardOpts
    • TWin32ClipboardOpts
    • TX11ClipboardOpts
  • BRL.D3D7Max2D
  • BRL.D3D9Max2D
  • BRL.DirectSoundAudio
  • BRL.EndianStream
  • Event

    • Introduction
    • TEvent
  • BRL.EventQueue
  • BRL.FileSystem
  • BRL.FreeAudioAudio
  • BRL.GLGraphics
  • BRL.GLMax2D
  • BRL.GNet
  • BRL.Graphics
  • BRL.Hook
  • IO

    • Introduction
    • MaxIO
  • BRL.JPGLoader
  • BRL.KeyCodes
  • LinkedList

    • Introduction
    • TList
    • TListEnum
    • TLink

    Map

    • Introduction
    • TMap
    • TIntMap
    • TIntKey
    • TPtrMap
    • TPtrKey
    • TStringMap
  • BRL.Math
  • Matrix

    • Introduction
    • SMat2D
    • SMat2F
    • SMat2I
    • SMat3D
    • SMat3F
    • SMat3I
    • SMat4D
    • SMat4F
    • SMat4I

    Max2D

    • Introduction
    • TImage

    MaxLua

    • Introduction
    • TLuaClass
    • TLuaObject
  • BRL.OGGLoader
  • BRL.OpenALAudio
  • ObjectList

    • Introduction
    • TObjectList
    • TObjectListEnumerator
    • TObjectListReverseEnumerator

    Pixmap

    • Introduction
    • TPixmap
    • TPixmapLoader
  • BRL.PNGLoader
  • BRL.PolledInput
  • Quaternion

    • Introduction
    • SQuatD
    • SQuatF
    • SQuatI
  • BRL.RamStream
  • Random

    • Introduction
    • TRandom

    Reflection

    • Introduction
    • TTypeId
    • TMember
    • TConstant
    • TField
    • TGlobal
    • TFunction
    • TMethod
  • BRL.Retro
  • BRL.Socket
  • BRL.SocketStream
  • BRL.StandardIO
  • Stream

    • Introduction
    • TCStream
    • TIO
    • TStream
    • TStreamException
    • TStreamFactory
    • TStreamReadException
    • TStreamWrapper
    • TStreamWriteException

    StringBuilder

    • Introduction
    • TStringBuilder
    • TSplitBuffer
  • BRL.System
  • BRL.SystemDefault
  • BRL.TextStream
  • BRL.TGALoader
  • ThreadPool

    • Introduction
    • TRunnable
    • TThreadPoolExecutor

    Threads

    • Introduction
    • TThread
    • TThreadData
    • TMutex
    • TSemaphore
    • TCondVar

    Timer

    • Introduction
    • TChrono
  • BRL.TimerDefault
  • Vector

    • Introduction
    • SVec2D
    • SVec2F
    • SVec2I
    • SVec3D
    • SVec3F
    • SVec3I
    • SVec4D
    • SVec4F
    • SVec4I

    Volumes

    • Introduction
    • TVolume
  • BRL.WAVLoader

Text

    CSV

    • Introduction
    • TCsvParser
    • TCsvHeader
    • TCsvRow
    • TCsvOptions
    • SCsvColumn

    Format

    • Introduction
    • TFormatter

    Ini

    • Introduction
    • TIni
    • TIniProperty
    • TIniSection

    JConv

    • Introduction
    • TJConv
    • TJConvBuilder
    • TJConvSerializer

    Json

    • Introduction
    • TJSON
    • TJSONObject
    • TJSONString
    • TJSONInteger
    • TJSONReal
    • TJSONBool
    • TJSONArray
    • TJSONNull
    • TJSONError
    • TJSONNumber

    Regex

    • Introduction
    • TRegEx
    • TRegExMatch
    • TRegExOptions
    • TRegExException

    TOML

    • Introduction
    • ETomlNodeType
    • TToml
    • ITomlNode
    • STomlDate
    • STomlDateTime
    • STomlTime
    • TTomlArray
    • TTomlInteger
    • TTomlString
    • TTomlTable
    • TTomlBoolean

    Xml

    • Introduction
    • TxmlDoc
    • TxmlNode
    • TxmlAttribute

MaxGUI

  • MaxGUI.MaxGUI
  • MaxGUI.Drivers
  • MaxGUI.Localization
  • MaxGUI.ProxyGadgets
  • MaxGUI.CocoaMaxGUI
  • MaxGUI.Win32MaxGUIEx

PUB

  • Pub.FreeJoy
  • Pub.FreeProcess
  • Pub.Glad
  • Pub.Glew
  • Joystick

    • Introduction
    • TJoystickDriver
  • Pub.Lua
  • Pub.OpenAL
  • Pub.OpenGLES
  • Pub.xmmintrin
  • Pub.ZLib

Crypto

    Digest

    • Introduction
    • TMessageDigest
    • TDigestRegister
    • TNoSuchAlgorithmException

    Cipher

    • Introduction
    • TCipher
    • TCipherMode
    • Crypto.AESCipher
    • Crypto.AnubisCipher
    • Crypto.BlowfishCipher
    • Crypto.CamelliaCipher
    • Crypto.Cast5Cipher
    • Crypto.DESCipher
    • Crypto.IdeaCipher
    • Crypto.KasumiCipher
    • Crypto.KhazadCipher
    • Crypto.KSeedCipher
    • Crypto.NoekeonCipher
    • Crypto.SerpentCipher
    • Crypto.SkipjackCipher
    • Crypto.TwofishCipher
    • Crypto.XTeaCipher

    CBCMode

    • Introduction
    • TCBCCipherMode

    CFBMode

    • Introduction
    • TCFBCipherMode

    CTRMode

    • Introduction
    • TCTRCipherMode

    ECBMode

    • Introduction
    • TECBCipherMode

    F8Mode

    • Introduction
    • TF8CipherMode

    LRWMode

    • Introduction
    • TLRWCipherMode

    OFBMode

    • Introduction
    • TOFBCipherMode

    XTSMode

    • Introduction
    • TXTSCipherMode

    Blake2BDigest

    • Introduction
    • TBlake2B_512
    • TBlake2B_384
    • TBlake2B_256
    • TBlake2B_160

    CRC32

    • Introduction
    • TCRC32

    MD5Digest

    • Introduction
    • TMD5

    Ripemd320Digest

    • Introduction
    • TRipemd320

    Ripemd356Digest

    • Introduction
    • TRipemd256

    Ripemd160Digest

    • Introduction
    • TRipemd160

    Ripemd128Digest

    • Introduction
    • TRipemd128

    SHA1Digest

    • Introduction
    • TSHA1

    SHA256Digest

    • Introduction
    • TSHA256

    SHA512Digest

    • Introduction
    • TSHA512

    SHA3Digest

    • Introduction
    • TSHA3_512
    • TSHA3_384
    • TSHA3_256
    • TSHA3_224

    TigerDigest

    • Introduction
    • TTiger

    WhirlpoolDigest

    • Introduction
    • TWhirlpool

    Crypto

    • Introduction
    • TCryptoRandom
    • TCryptoSecretBox
    • TCryptoHash
    • TCryptoSign
    • TCryptoKeyExchange
    • TCryptoKey
    • TCryptoHashKey
    • TCryptoSecretBoxKey
    • TCryptoSignature
    • TCryptoExchangeKeyPair
    • TCryptoSessionKeyPair
    • TCryptoNPacket
    • TCryptoKK1Packet
    • TCryptoKK2Packet
    • TCryptoXX1Packet
    • TCryptoXX2Packet
    • TCryptoXX3Packet
    • TCryptoExchangeState
    • TCryptoPWHashMasterKey
    • TCryptoPWHashStoredKey
    • TCryptoPasswordHash

SDL

    SDL

    • Introduction
    • TSDLStream

    SDLSystem

    • Introduction
    • TSDLMultiGesture

    SDLVideo

    • Introduction
    • TSDLDisplay
    • TSDLDisplayMode
    • TSDLGLContext
    • TSDLWindow

    SDLRender

    • Introduction
    • TSDLRenderer
    • TSDLTexture

    SDLTimer

    • Introduction
    • TSDLTimer

    SDLHaptic

    • Introduction
    • TSDLHaptic
    • TSDLHapticCondition
    • TSDLHapticConstant
    • TSDLHapticCustom
    • TSDLHapticDirection
    • TSDLHapticEffect
    • TSDLHapticLeftRight
    • TSDLHapticPeriodic
    • TSDLHapticRamp

    SDLSensor

    • Introduction
    • TSDLSensor
  • SDL.SDLFreeAudio
  • SDL.GLSDLMax2D
  • SDL.GL2SDLMax2D
  • SDL.D3D9SDLMax2D
  • SDLVirtualJoystick

    • Introduction
    • TVirtualJoystick

Steam

    SteamSDK

    • Introduction
    • TSteamClient
    • TSteamUtils
    • TSteamUserStats
    • TSteamFriends
    • TSteamUGC
    • ISteamFriendsListener
    • ISteamUGCListener
    • ISteamUserStatsListener
    • ISteamUtilsListener
    • EItemPreviewType
    • EPersonaChange
    • EPersonaState
    • ERemoteStoragePublishedFileVisibility
    • EUGCMatchingUGCType
    • EUGCQuery
    • EUserRestriction
    • EUserUGCList
    • EWorkshopFileType
Edit

BRL.Random

The random module contains commands for generating random numbers.

The numbers generated are not really random, as without special hardware support a software algorithm cannot produce 'real' random numbers.

Instead, the algorithm produces values that merely appear to be random. In reality, each generated value actually depends on the previously generated value.

You can set the 'state' of the random number generator using the SeedRnd command. A common practice is to seed the random number generator with the system time when your program starts up, for example:

SeedRnd MilliSecs()

This ensures that the random number generator does not start in the same state each time your program is run, which would cause it to produce the same sequence of random numbers.

Types

TypeDescription
TRandomRandom number generator

Functions

Function CreateRandom:TRandom()

Creates a new TRandom instance.


Function CreateRandom:TRandom(seed:Int)

Creates a new TRandom instance with the given seed.


Function RndFloat:Float()

Generate random float

Returns

A random float in the range 0 (inclusive) to 1 (exclusive)

Example

' RndFloat.bmx
' Two players take turns shooting at a target. The first hit wins.
' Player 1 hits 30% of the time, player 2 hits 40%.
' What is the probability that player 1 wins?

SuperStrict

Function winner:Int()   ' play game once, return winner 1 or 2
    Repeat
        If RndFloat() < 0.3 Then Return 1
        If RndFloat() < 0.4 Then Return 2
    Forever
End Function

Local count:Int[3]

Local trials:Int = 1000000

For Local n:Int = 1 To trials
    count[ winner() ] :+ 1
Next

Print "Estimated probability = " + ( Float( count[1] ) / Float( trials ) )
Print
Print "    Exact probability = " + ( 15.0 / 29.0 )


Function RndDouble:Double()

Generate random double

Returns

A random double in the range 0 (inclusive) to 1 (exclusive)

Example

' RndDouble.bmx
' Two players take turns shooting at a target. The first hit wins.
' Player 1 hits 30% of the time, player 2 hits 40%.
' What is the probability that player 1 wins?

SuperStrict

Function winner:Int()   ' play game once, return winner 1 or 2
    Repeat
        If RndDouble() < 0.3 Then Return 1
        If RndDouble() < 0.4 Then Return 2
    Forever
End Function

Local count:Int[3]

Local trials:Int = 1000000

For Local n:Int = 1 To trials
    count[ winner() ] :+ 1
Next

Print "Estimated probability = " + ( Double( count[1] ) / Double( trials ) )
Print
Print "    Exact probability = " + ( 15.0 / 29.0 )


Function Rnd:Double(minValue:Double = 1, maxValue:Double = 0)

Generate random double

The optional parameters allow you to use Rnd in 3 ways:

FormatResult
`Rnd()`Random double in the range 0 (inclusive) to 1 (exclusive)
`Rnd(x)`Random double in the range 0 (inclusive) to n (exclusive)
`Rnd(x,y)`Random double in the range x (inclusive) to y (exclusive)

Returns

A random double in the range min (inclusive) to max (exclusive)

Example

' Rnd.bmx
' Use Rnd() to estimate area inside the unit circle x^2 + y^2 = 1.

SuperStrict

Local totalpoints:Int = 1000000
Local inpoints:Int

For Local n:Int = 1 To totalpoints
    Local x:Double = Rnd( -1.0, 1.0 )               ' Generate random point in 2 by 2 square.
    Local y:Double = Rnd( -1.0, 1.0 )

    If x*x + y*y < 1.0 Then inpoints :+ 1     ' point is inside the circle
Next

' Note: Ratio of areas circle/square is exactly Pi/4.

Print "Estimated area = " + ( 4.0 * Double(inpoints)/Double(totalpoints) )
Print
Print "    Exact area = " + Pi     '  4 * Pi/4, compare with estimate


Function Rand:Int(minValue:Int, maxValue:Int = 1)

Generate random integer

The optional parameter allows you to use Rand in 2 ways:

FormatResult
`Rand(x)`Random integer in the range 1 to x (inclusive)
`Rand(x,y)`Random integer in the range x to y (inclusive)

Returns

A random integer in the range min (inclusive) to max (inclusive)

Example

' Rand.bmx
' Toss a pair of dice. Result is in the range 1+1 to 6+6.
' Count how many times each result appears.

SuperStrict

Local count:Int[13]

For Local n:Int = 1 To 3600
    Local toss:Int = Rand(1,6) + Rand(1,6)
    count[toss] :+ 1
Next

For Local toss:Int = 2 To 12
    Print LSet(toss, 5)+count[toss]
Next


Function SeedRnd(seed:Int)

Set random number generator seed

Example

' RndSeed.bmx and SeedRnd.bmx ( one example for both )
' Get/Set random number seed.

SuperStrict

SeedRnd MilliSecs()

Local seed:Int = RndSeed()

Print "Initial seed="+seed

For Local k:Int = 1 To 10
Print Rand(10)
Next

Print "Restoring seed"

SeedRnd seed

For Local k:Int = 1 To 10
Print Rand(10)
Next


Function RndSeed:Int()

Get random number generator seed

Used in conjunction with SeedRnd, RndSeed allows you to reproduce sequences of random numbers.

Returns

The current random number generator seed

Example

' RndSeed.bmx and SeedRnd.bmx ( one example for both )
' Get/Set random number seed.

SuperStrict

SeedRnd MilliSecs()

local seed:int = RndSeed()

Print "Initial seed="+seed

For local k:int = 1 To 10
    Print Rand(10)
Next

Print "Restoring seed"

SeedRnd seed

For local k:int = 1 To 10
    Print Rand(10)
Next


← BRL.RamStreamTRandom →
  • Types
  • Functions
    • Function CreateRandom:TRandom()
    • Function CreateRandom:TRandom(seed:Int)
    • Function RndFloat:Float()
    • Function RndDouble:Double()
    • Function Rnd:Double(minValue:Double = 1, maxValue:Double = 0)
    • Function Rand:Int(minValue:Int, maxValue:Int = 1)
    • Function SeedRnd(seed:Int)
    • Function RndSeed:Int()
BlitzMax
Docs
Getting StartedDownloadsAbout
Community
ResourcesSyntaxBomb Forums
More
GitHubStarChat on Discord
Copyright © 2023 Bruce A Henderson