random generator with specific outputs - random

my teacher ask me to create a c++ program that generate n number of random integers where n is also random and will output the maximum,minimum random integer and average. Im having a hard time on this one

Related

If two random numbers generated at exactly same time using Math.random(), would they be same or different?

I want to predict the random number generated with Math.random() function.
I studied an article that the algorithm behind random number generator function takes the time in mili seconds an input, and generate a random number.
So, is this possible to predict the next random number?
I am playing a game named as dragon/tiger. Each dragon and tiger has 13 numbers. The numbers for both are both randomly generated after every 15 seconds. The side having higher number wins. Is there any way to predict which side will win(what number would be generated next)?

Random number generator with freely chosen period

I want a simple (non-cryptographic) random number generation algorithm where I can freely choose the period.
One candidate would be a special instance of LCG:
X(n+1) = (aX(n)+c) mod m (m,c relatively prime; (a-1) divisible by all prime factors of m and also divisible by 4 if m is).
This has period m and does not restrict possible values of m.
I intend to use this RNG to create a permutation of an array by generating indices into it. I tried the LCG and it might be OK. However, it may not be "random enough" in that distances between adjacent outputs have very few possible values (i.e, plotting x(n) vs n gives a wrapped line). The arrays I want to index into have some structure that has to do with this distance and I want to avoid potential issues with this.
Of course, I could use any good PRNG to shuffle (using e.g. Fisher–Yates) an array [1,..., m]. But I don't want to have to store this array of indices. Is there some way to capture the permuted indices directly in an algorithm?
I don't really mind the method ending up biased w.r.t choice of RNG seed. Only the period matters and the permuted sequence (for a given seed) being reasonably random.
Encryption is a one-to-one operation. If you encrypt a range of numbers, you will get the same count of apparently random numbers back. In this case the period will be the size of the chosen range. So for a period of 20, encrypt the numbers 0..19.
If you want the output numbers to be in a specific range, then pick a block cipher with an appropriately sized block and use Format Preserving Encryption if needed, as #David Eisenstat suggests.
It is not difficult to set up a cipher with almost any reasonable block size, so long as it is an even number of bits, using the Feistel structure. If you don't require cryptographic security then four or six Feistel rounds should give you enough randomness.
Changing the encryption key will give you a different ordering of the numbers.

Uniformly distribute N items into C bins

I have a number of items N and I want to uniformly distribute them among a number of C bins. My first though was to generate a random double number between 0 and 1 and then multiply it with the number N but it's not working as i expected. We are currently working on a Java project but a general algorithm would be fine.
Bins have no specific capacity and numbers don't have weights
You have not specified what you mean by "uniformly distribute ".
There are M=CN variants of distribution of N items into C bins. So you can random integer in range 0..M-1 and represent it in C-ary numeral system to get random combination.
Given that all items and bins are identical we can use the following simple approach, which is definitely not the most efficient way to go but it is easy and works.
Create a vector containing the sequence 1 to N and use a function to randomly shuffle the values (e.g., Collections.shuffle(values)). Then the first N/C items are placed in the first bin, the following N/C items in the second one, etc..
Example, we have N=10 items and C=2 bins. We create the vector val = {1,2,3,4,5,6,7,8,9,10} and using a random shuffle function gives val = {4,8,2,1,9,10,5,3,6,7}. Then use this to get the following two bins
bin1: {4,8,2,1,9} and bin2: {10,5,3,6,7}

Generating a stateless pseudo-random number from four integers

For an implementation of Perlin noise, I need to select a vector from a static list of n vectors for each integer coordinate in 3D space. This boils down to generating a pseudo random number in 1..n from four signed integer values x, y, z and seed.
unsigned int pseudo_random_number(int x, int y, int z, int seed);
The algorithm should be stateless, i.e., return the same number each time it is called with the same input values.
An existing Perlin noise implementation I looked at multiplies each integer with a large prime, adds the results, does some bit manipulation on it and takes the reminder of a division by n. I don't want to just copy this because I don't understand a few things about it:
How are the primes selected?
Why is the additional bit manipulation done?
How do I know if this is „sufficiently pseudo-random“ to generate a visually pleasing result?
I looked for explanations of how a PRNG works but I couldn't find anything about multiple input values.
If you have arbitrary precision pseudo-random number generation then you can just concatenate the four inputs (x,y,z,seed) and call your pseudo-random number generator function on this input to get the "next" pseudo-random number which will serve as your random number. (and then take the appropriate number of high bits if you want to have a random number between 1 and n).
The implementation you mentioned uses the fact that different large prime numbers, modulo n, produce essentially uncorrelated results (modulo n) when multiplied with input integers. Of course you need your input integers to not all have a universal common divisor with n for this to work. This is why the additional bit manipulation is done, so that if all of your input integers are divisible by k and n is divisible by k, the remainder modulo n will not automatically be divisible by k as well. At any rate, people have put a lot of thought into established pseudo-random number generators so my advice to you is that you trust that they considered all the potential issues and that their generator is "good" if there is a large crowd that uses it without complaints.

how to generate random numbers with a specified mean

I have a question like, I should genearate 'k' random numbers lets say it is from 1 to 1000. But the generated numbers should have a mean value of 300. I used rand() function to generate random numbers. But I am stuck with the mean value. How can I do so that the numbers generated have a mean value.
I'd generate k-1 random numbers, and then set the K number to be (mean*k-[sum of all the numbers you generated so far]).
Unfortunately, the C standard does not guarantee that the random numbers are uniform (it doesn't specify any distribution, for that matter), so the only way to do it is to generate the 1000 numbers in advance, calculate the mean (M) and subtract M-300 from every element

Resources