Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
So when using, for example, rand(10), how does ruby generate the random number? I have very little knowlege about random number generation techniques, so I would like to get to know them better.
Ruby is open-source. I'll demonstrate how to locate the PRNG (pseudo random number generator) code, as there's no way to generate truly random numbers using a deterministic CPU.
Looking at the repository, we see a suspiciously-named file, random.c. Looking inside, it's in C, but that's ok, it has comments. The first function is genrand_real, calling genrand_int32, which takes a struct MT. This function is defined in mt19937.c and looking at that file, it uses bitwise operations to get the next state of the random number generator and applies more bitwise operators to generate the number desired.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm working on a pseudorandom number generator for an assignment and I'm having a hard time wrapping my head around how state is used in it. What does it mean to advance to the next one? I'm not looking for tips on implementation, just an explanation of the concept. Thanks!
A PRNG generates a sequence of numbers.
To calculate the next number, you have some internal state (variables set to specific values, if you will). That's the state referred to in the context of PRNG. This state can often be represented by just a single number.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I am currently reading a programming text book and as I discover different algorithms used in the book I'm finding it necessary to understand how they work by working through them. Is there a standard & efficient way to work through simple algorithms on paper?
Write the algorithm down on the paper. Write the corresponding graphs and variables that you use in algorithm.
Now follow algorithm step by step and note what changed with variables and graphs etc.
Time slices. Make a table, where the column headers are variables involved, and row headers are step numbers. Fill in row zero with initial values if any, and each row represents the result of the current step on the previous row.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm doing a project on single precision floating numbers. I was wondering in what fields or areas are these concepts used? Thanks in advance.
For anything requiring larger range than available with integers, and where limited accuracy of number representation isn't important enough to use longer floats. In terms of accuracy, nothing beats integer or fixed point, at the price of their limited range. Say if i wanted cosmological distances in a unit which can be used for both nearer and far objects at the same time, i could think of using those - after all, i'd be mostly interested in the most significant parts of the distance, not in the submillimeter portion.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to display the outcome of throwing a dice with equal probability of 1,2,3,4,5,6 ?
Random no does not display with equal probability.
Thanks in advance.
If you have a reasonably good PRNG (pseudo-random number generator), you should be able to do something like (where % is the modulo operator):
print( good_prng() % 6 + 1 )
Some basic PRNGs aren't very random in the lower order bits. Without knowing what language and PRNG you're using, it's hard to say if this is the reason for what you're observing.
In Python (asked for in comment):
import random
print( random.randint(1,6) )
Python's random module uses a Mersenne twister, which is a very good PRNG.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Are there any algorithms for finding the maximum value of a continuous function, which is proofed to be bounded upside?
For example, a function similar to sin.
I think Newton's method and Mid-point method are for finding a fixed value, any other methods for finding maximum value?
For general functions that are "Lipschitz-continuous" (meaning that the output changes by at most a constant factor times the change in input) see e.g. http://link.springer.com/article/10.1007%2FBF00938542#page-1 and http://link.springer.com/article/10.1007%2Fs10898-012-9937-9#page-1 . If your function is arbitrary continuous and not Lipschitz-continuous, then in theory the function could change to an arbitrarily high or low value over an arbitrarily small region, so provable global optimization is very hard.