Randomization Question - Get a Clear & Informative Response

  • Thread starter Thread starter hoangr
  • Start date Start date
Click For Summary

Discussion Overview

The discussion centers around the concept of randomness in programming, specifically focusing on how random functions generate outputs and the role of seeds in random number generation. Participants explore both mathematical and programming perspectives, touching on algorithms, statistical properties, and historical methods of generating random numbers.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • Ryan initiates the discussion by seeking clarity on the programming aspect of random functions and their outputs.
  • One participant explains that random functions typically rely on deterministic series with long repeat cycles, emphasizing the importance of specifying a seed for reproducibility.
  • Another participant notes that usual random number generators produce sequences that appear random when subjected to statistical tests.
  • A participant shares a code snippet from the original C textbook, describing how the rand function operates and the necessity of initializing the seed value for consistent results.
  • The same participant mentions potential issues with thread safety in the provided code, acknowledging its outdated nature.
  • Ryan expresses gratitude for the insights, particularly regarding the significance of seeds in programming random algorithms.

Areas of Agreement / Disagreement

Participants generally agree on the role of seeds in random number generation and the deterministic nature of typical random functions. However, there is no consensus on the best practices for implementing random functions, particularly regarding thread safety and the use of outdated code.

Contextual Notes

Some limitations include the reliance on specific programming languages and the historical context of random number generation methods. The discussion does not resolve the complexities surrounding the implementation of random functions in modern programming environments.

Who May Find This Useful

This discussion may be useful for programmers, computer scientists, and anyone interested in the mathematical foundations of randomness and its applications in coding.

hoangr
Messages
2
Reaction score
0
Hi,

I was looking into the mathematical aspect of what makes something 'random', but never asked about the programming view. What happens in a random function that generates an output? Although I still haven't found an answer to my question, I've decided to post it here hoping that I could get a clear and informative response (sometimes Google doesn't cut it).

Thanks,
Ryan
 
Technology news on Phys.org
A random function depends on the randomizer - usually it is a deterministic series with a very long repeat cycle. That's why you usually need to specify a "seed". For instance, if I use the rand() function in MATLAB for n data points, then repeat it twice, the three sets of results will be identical though they look random within each set.

Fancy systems use some physical method like the 10th decimal place in a sensitive temperature gauge of wind speed or something like that.

In the bad old day we used to use a table of "random numbers" that you could buy. It came as part of a book of tables for all those other things you use a computer for now like sine and cosine and the normal distribution and logs.

Randomizing algorithms are an important part of cryptography.
 
Usual random number generators will produce a sequence of numbers (uniformly distributed between 0 and 1) which look random when various statistical tests are applied.
 
K&R (the original C programmers' textbook that started me down this dark path) provides this as the source code for rand ()

Code:
unsigned long int next = 1;

int rand(void)
{
    next = next * 1103515245 + 12345;
    return (unsigned int)(next/65536) % 32768;
}

It requires initialisation of the seed value named "next", and will always return the same sequence of random numbers for the same seed value. Hence, usually you initialise this at the start of your program with some "random" value such as the system time (full date and time, obviously).

edit: Being able to set the seed allows for debugging etc, by being able to guarantee the same results are returned.

I note that it doesn't appear to be thread-safe :-) [edit: if your program spawns multiple threads for working concurrently, the value of next is shared by all]

edit: Yes this is not regarded as current code. I know it's bad code. Don't all jump on me! :)
 
Last edited:
Wow, thanks for those informative responses! For some reason I didn't even consider seeds being a factor in programming these algorithms.

@rorix_bw: Thanks for the code snippet, I've been looking for the source code in other languages and wasn't successful. :P
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
3K
Replies
19
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 40 ·
2
Replies
40
Views
3K
Replies
7
Views
5K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 88 ·
3
Replies
88
Views
10K
Replies
1
Views
2K
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K