summaryrefslogtreecommitdiff
path: root/lib/libc/stdlib/rand.c
Commit message (Collapse)AuthorAgeFilesLines
* rand(3): Replace implementation with one backed by random(3) algorithmConrad Meyer2020-02-011-5/+58
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | rand(3)'s standard C API is extremely limiting, but we can do better than the historical 32-bit state Park-Miller LCG we've shipped since 2001: r73156. The justification provided at the time for not using random(3) was that rand_r(3) could not be made to use the same algorithm. That is still true. However, the irrelevance of rand_r(3) is increasingly obvious. Since that time, POSIX has marked the interface obsolescent. rand_r(3) never became part of the standard C library. If not for API compatibility reasons, I would just remove rand_r(3) entirely. So, I do not believe it is a problem for rand_r(3) and rand(3) to diverge. The 12 ABI is maintained with compatibility definitions, but this revision does subtly change the API of rand(3). The sequences of pseudorandom numbers produced in programs built against new versions of libc will differ from programs built against prior versions of libc. Reviewed by: kevans, markm MFC after: no Relnotes: yes Differential Revision: https://reviews.freebsd.org/D23290 Notes: svn path=/head/; revision=357382
* libc: Delete unused rand.c ifdef TEST codeConrad Meyer2020-01-201-34/+0
| | | | Notes: svn path=/head/; revision=356934
* Deprecate sranddev(3) APIConrad Meyer2019-12-141-15/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | It serves no useful purpose and wasn't as popular as its equally meritless cousin, srandomdev(3). Setting aside the problems with rand(3) in general, the problem with this interface is that the seed isn't shared with the caller (other than by attacking the output of the generator, which is trivial, but not a hallmark of pleasant API design). The (arguable) utility of rand(3) or random(3) is as a semi-fast simulation generator which produces consistent results from a given seed. These are mutually at odd. Furthermore, sometimes people got the mistaken impression that a high quality random seed meant a weak generator like rand(3) or random(3) could be used for things like cryptographic key generation. This is absolutely not so. The API was never part of a standard and was not widely used in tree. Existing in-tree uses have all been removed. Possible replacement in out of tree codebases: char buf[3]; time_t t; time(t); strftime(buf, sizeof(buf), "%S", gmtime(&t)); srand(atoi(buf)); Relnotes: yes Notes: svn path=/head/; revision=355747
* General further adoption of SPDX licensing ID tags.Pedro F. Giffuni2017-11-201-0/+2
| | | | | | | | | | | | | | | | | Mainly focus on files that use BSD 3-Clause license. The Software Package Data Exchange (SPDX) group provides a specification to make it easier for automated tools to detect and summarize well known opensource licenses. We are gradually adopting the specification, noting that the tags are considered only advisory and do not, in any way, superceed or replace the license texts. Special thanks to Wind River for providing access to "The Duke of Highlander" tool: an older (2014) run over FreeBSD tree was useful as a starting point. Notes: svn path=/head/; revision=326025
* 1) Unifdef USE_WEAK_SEEDING since it is too obsolete to support and makesAndrey A. Chernov2016-05-291-41/+11
| | | | | | | | | | | | | | | | reading hard. 2) Instead of doing range transformation in each and every function here, do it single time directly in do_rand(). One "mod" operation overhead is not a big deal, but the code looks nicer and possible future functions additions or PRNG change do not miss range transformations neither have unneeded ones. 3) Use POSIX argument types for visible functions (cosmetic). MFC after: 1 week Notes: svn path=/head/; revision=300956
* libc: do not include <sys/types.h> where <sys/param.h> was already includedAndriy Voskoboinyk2016-04-181-1/+0
| | | | | | | | | According to style(9): > normally, include <sys/types.h> OR <sys/param.h>, but not both. (<sys/param.h> already includes <sys/types.h> when LOCORE is not defined). Notes: svn path=/head/; revision=298226
* Use ANSI C prototypes. Eliminates -Wold-style-definition warnings.Craig Rodrigues2015-09-201-4/+3
| | | | Notes: svn path=/head/; revision=288030
* Style fix noted by bde@Andrey A. Chernov2013-07-041-4/+8
| | | | Notes: svn path=/head/; revision=252698
* After fixing ranges restore POSIX requirement: rand() call withoutAndrey A. Chernov2013-07-041-1/+6
| | | | | | | | srand() must be the same as srand(1); rand(); (yet one increment) Notes: svn path=/head/; revision=252668
* In addition to prev. commit, for repeated rand_r(3) calls don't forgetAndrey A. Chernov2013-07-031-1/+5
| | | | | | | | | | to compensate back at the end incremented at the start internal state. MFC after: 2 weeks Notes: svn path=/head/; revision=252648
* 1) POSIX requires rand(3) return values to be in the [0, RAND_MAX] range,Andrey A. Chernov2013-07-031-4/+16
| | | | | | | | | | | | | | | | | | | but ACM formula we use have internal state (and return value) in the [1, 0x7ffffffe] range, so our RAND_MAX (0x7fffffff) is never reached because it is off by one, zero is not reached too. Correct both RAND_MAX and rand(3) return value, shifting last one to the 0 by 1 subtracted, resulting POSIXed [0, 0x7ffffffd(=new RAND_MAX)] range. 2) Add a checks for not overflowing on too big seeds. It may happens on the machines, where sizeof(unsigned int) > 32 bits. Reviewed by: bde [1] MFC after: 2 weeks Notes: svn path=/head/; revision=252608
* Renumber clauses to reduce diffs to other versionsEd Maste2013-06-131-1/+1
| | | | | | | | NetBSD, OpenBSD, and Android's Bionic number the clauses 1 through 3, so follow suit to make comparison easier. Notes: svn path=/head/; revision=251672
* Replace access to /dev/random with the kernel pseudo-random numberXin LI2013-04-021-21/+12
| | | | | | | | | | | source sysctl(KERN_ARND) and remove the fallback code. Obtained from: OpenBSD Reviewed by: secteam MFC after: 1 month Notes: svn path=/head/; revision=249035
* Remove undefined behavior from sranddev() andEitan Adler2012-10-091-2/+1
| | | | | | | | | | | | | | | srandomdev(). This doesn't actually work with any modern C compiler: In particular, both clang and modern gcc verisons silently elide any xor operation with 'junk'. Approved by: secteam MFC after: 3 days Notes: svn path=/head/; revision=241373
* libc: Use O_CLOEXEC for various internal file descriptors.Jilles Tjoelker2012-09-291-1/+1
| | | | | | | | | | | | This fixes a race condition where another thread may fork() before CLOEXEC is set, unintentionally passing the descriptor to the child process. This commit only adds O_CLOEXEC flags to open() or openat() calls where no fcntl(fd, F_SETFD, FD_CLOEXEC) follows. The separate fcntl() call still leaves a race window so it should be fixed later. Notes: svn path=/head/; revision=241046
* Fix typo in the commentAndrey A. Chernov2007-12-111-1/+1
| | | | Notes: svn path=/head/; revision=174541
* Per Regents of the University of Calfornia letter, remove advertisingWarner Losh2007-01-091-4/+0
| | | | | | | | | clause. # If I've done so improperly on a file, please let me know. Notes: svn path=/head/; revision=165903
* Back out "drop first N values" method of removing monotonically increasedAndrey A. Chernov2003-02-171-7/+1
| | | | | | | | | | | | | | | | | | seed->first value correlation. It breaks rand_r()... Other possible methods like shuffling inside aray will breaks rand_r() too, because it assumes only one word state, i.e. nothing extra can be added after seed assignment in srand(). BTW, for old formulae seed->first value correlation is not so monotonically increased as with other Linear Congruential Generators of this type only becase arithmetic overflow happens. But overflow affects distribution and lower bits very badly, as many articles says, such type of overflow not improves PRNG. So, monotonically increased seed->first value correlation problem remains... Notes: svn path=/head/; revision=111026
* Since we drop NSHUFF values now, set default seed to what it becomesAndrey A. Chernov2003-02-051-3/+3
| | | | | | | after srand(1) Notes: svn path=/head/; revision=110421
* For rand(3) and random(3) TYPE_0 drop NSHUFF values right after srand{om}()Andrey A. Chernov2003-02-041-1/+7
| | | | | | | | | | to remove part of seed -> 1st value correlation. Correlation still remains because of algorithm limits. Note that old algorithm have even stronger correlation, especially in the lower bits area, but not eye-visible, as current one. Notes: svn path=/head/; revision=110321
* Park & Miller PRNG can be safely initialized with any value but 0 and stuckAndrey A. Chernov2003-02-031-1/+4
| | | | | | | | | | at 0 as designed. Its BSD adaptation tries to fight it by mapping 0 to 2147483647 after calculation, but this method not works since 2147483647 seed returns to 0 again on the next interation. Instead of after calculation mapping, map 0 to another value _before_ calculation, so it never stucks. Notes: svn path=/head/; revision=110280
* For some combinations of variable sizes and RAND_MAX value rand_r()Andrey A. Chernov2003-02-021-2/+4
| | | | | | | may store less amount bits for seed, than available. Fix it. Notes: svn path=/head/; revision=110236
* Add missing #include "namespace.h".Tim J. Robbins2003-01-031-0/+1
| | | | Notes: svn path=/head/; revision=108625
* Fix the style of the SCM ID's.David E. O'Brien2002-03-221-2/+2
| | | | | | | I believe have made all of libc .c's as consistent as possible. Notes: svn path=/head/; revision=92986
* Add a long-overdue nail to the deprecated /dev/urandom interfaceMark Murray2001-10-301-1/+1
| | | | | | | | by asking some things that need unpredictable numbers to read /dev/random instead. Notes: svn path=/head/; revision=85752
* urandom(4) -> random(4) in comments.Dima Dorfman2001-06-071-1/+1
| | | | | | | | | | PR: 27858 Submitted by: Yoshihiro Koya <Yoshihiro.Koya@math.yokohama-cu.ac.jp> Reviewed by: md5(1) Approved by: markm Notes: svn path=/head/; revision=77851
* Add #include "un-namespace.h"Andrey A. Chernov2001-04-231-0/+1
| | | | Notes: svn path=/head/; revision=75863
* srand*dev() fallback code: change ^getpid() to ^(getpid() << 16) to allowAndrey A. Chernov2001-04-231-1/+1
| | | | | | | change of high word part too to produce more interesting seed distribution. Notes: svn path=/head/; revision=75862
* Add sranddev() since srand() is not vary much with seed, typical timeAndrey A. Chernov2001-04-231-0/+34
| | | | Notes: svn path=/head/; revision=75845
* Use formula with better random distribution for rand()Andrey A. Chernov2001-02-271-0/+26
| | | | | | | | | Even better formula from random() could not be intetgrated because rand_r() supposed to store its state in the single variable (but table needed for random() algorithm integration). Notes: svn path=/head/; revision=73156
* Added Posix rand_r function.Wes Peters1999-05-241-1/+52
| | | | Notes: svn path=/head/; revision=47474
* BSD 4.4 Lite Lib SourcesRodney W. Grimes1994-05-271-0/+54
Notes: svn path=/cvs2svn/branches/unlabeled-1.1.1/; revision=1573