GithubHelp home page GithubHelp logo

hiraethbbs / renegade.random Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 23 KB

Random functions using the current os's built in methods.

License: GNU General Public License v3.0

Pascal 99.76% Makefile 0.24%
random-bytes renegade renegade-bbs arc4random-buf mersenne-twister urandom random free-pascal fpc free-pascal-3

renegade.random's Introduction

Free Pascal Random Bytes

This file is a part of Renegade BBS.

This will only work with Free Pascal version 3+. I Think namespacing is a good idea, and FPC 3+ is not hard to get your hands on for any platform. In fact, I believe its the most cross platform program that I have ever found, including ScummVM.

From the Free Pascal website :

Free Pascal is a 32, 64 and 16 bit professional Pascal compiler. It can target many processor architectures: Intel x86 (including 8086), AMD64/x86-64, PowerPC, PowerPC64, SPARC, ARM, AArch64, MIPS and the JVM. Supported operating systems include Linux, FreeBSD, Haiku, Mac OS X/iOS/iPhoneSimulator/Darwin, DOS (16 and 32 bit), Win32, Win64, WinCE, OS/2, MorphOS, Nintendo GBA, Nintendo DS, Nintendo Wii, Android, AIX and AROS. Additionally, support for the Motorola 68k architecture is available in the development versions.

Usage

Make sure to include {$mode objfpc}{$H+} in your header or compile with -S2 -Sh switches.

Program RandomTest;
{$mode objfpc}{$H+}

uses
  Renegade.Random,
  Classes;

var
  R : TRandom;
  S : AnsiString;
  T : TBytes;
begin
  R := TRandom.Init;
  S := R.GetString(22);
  T := R.GetBytes(22);
  R.Free;

You can also use a custom random generator. It needs to implement Random.RandomInterface;

Program RandomTest;
{$mode objfpc}{$H+}

uses
  Renegade.Random,
  MyCustomGenerator,
  Classes;

var
  R : TRandom;
  S : AnsiString;
  T : TBytes;
begin
  R := TRandom.Init;
  R.RandomGenerator := MyCustomGenerator.Create;
  S := R.GetString(22);
  T := R.GetBytes(22);
  R.SetDefaultGenerator; // To get back to the default Generator.
  S := R.GetString(22);
  T := R.GetBytes(22);
  R.Free;
  • On Linux systems TRandom trys to use syscall(SYS_getrandom) if that fails it reads from /dev/urandom and then /dev/random before failing.
  • On Windows systems TRandom uses Windows built in CryptGenRandom function.
  • On BSD systems TRandom uses arc4random_buf. (Done : read from arc4random_buf this is the same on Mac systems, because Mac = FreeBSD)

TRandom will fall back on Free Pascal's Random function which uses the Mersenne Twister algorithm to get random bytes.

renegade.random's People

Contributors

sikofitt avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.