Function to generate pseudo ramdom sequence [duplicate] - random

This question already has answers here:
Unique (non-repeating) random numbers in O(1)?
(22 answers)
How to generate a random alpha-numeric string
(46 answers)
Closed 6 years ago.
Lets assume an ordered finite sequence of integers A = {0,1,2, ... k}.
Im looking for a (seeded) function f: A -> A where the image of f seems to be ramdom, but every element is hit exactly once (bijective).
It's importatant that someone who has access to some results of f should neither be able to estimate if he's looking at direkt neighbours f(n), f(n+1) nor should he be able to guess the next result.
One idea would be to map a range(1, k) on a shuffle(range(1, k)). But this seems very low in performance (for large k) and does miss the basic idea of a generator. The first element in the sequece should be as expensive to calculate as the n-th element.
Difference to 'normal' randomgenerators
Read carefully before marking as dublicated. I'm not looking for randomly picking from a range where collisions are possible. Also I want to calculate the n-th element without calculating all n-1elements before, like I would have to using the MT or Xorshift.

Related

generate all subsets of set given by binary representation of integer in c++ [duplicate]

This question already has answers here:
How do i generate all possible subsets of an binary string?
(5 answers)
Closed 4 years ago.
In c++ I am searching for an efficient algorithm to generate all integers such that their binary representation is a subset of a set which is given by the binary representation of integer N. By efficient I mean that I don't want to for-loop through all integers smaller than N to check whether they are subsets, mainly because N could be very large.
An idea I had was to generate all possible subsets of an integer corresponding to the Hamming weight of N and then shift them to the correct positions using <<, but I am so far failing to find a good way how to do this.
Example:
For set 110100 given by integer N=52 all possible subsets would be:
{000100,010000,010100,100000,100100,110000}
corresponding to integers {4,16,20,32,36,48}, which is what I want to generate.
Let P be popcount(N), the number of bits that are set. The number of results is then 2P - 2.
Treat N as a boolean array (bits). For each bit which is set, generate two subsets: one with that bit set and one without it. This can be done recursively until no bits are set.
Finally, discard the original N from the results, and also 0 (as per your example).
The time complexity is linear in the size of the output, i.e. O(2P).

Algorithm to calculate N paths from K set [duplicate]

This question already has answers here:
Algorithm to return all combinations of k elements from n
(77 answers)
Closed 7 years ago.
I'm looking for an algorithm that can calculate N paths from K set where N=10 and K=32. This is not a combinatory algo, I don't need every number in every position.
So basically I need to be able to find the distinct paths of up to 10 elements in a collection of 32.
Edit:
If I have a set of numbers
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}
I would like to get every unique path of 10. So an example would be
Path 1 {1,2,3,4,5,6,7,8,9,10}
Path 2 {1,2,3,4,5,6,7,8,9,11}
Path 3 {1,2,3,4,5,6,7,8,9,12}
Down the line I would like something like this:
Path N { 3,5,6,8,9,12,14,17,18,19 }
Since I need every possible path consisting of 10 elements from the larger set.
This has been answered here :
Algorithm to return all combinations of k elements from n
Note that you will get 64512240 paths. You may want to think a bit about it before filling your RAM with them.

What is the average time of running line 4 of MINIMUM function? [duplicate]

This question already has answers here:
Number of assignments necessary to find the minimum value in an array?
(4 answers)
Closed 8 years ago.
how much be run the line 4 of this function in average? O(logn) or O(n)
Minimum(A,n)
1. min= +inf
2. for i:1 to n
3. do if min>A[i]
4. then min=A[i]
Assuming the array is not sorted (otherwise see below):
It is O(n) because:
You have to look at each entry of the array at least once otherwise an entry you did not look at might be even smaller
You have to look at each entry at most once because you never forget what is the current minimum.
An array is direct access (no seek time)
If the array is sorted then finding the minimum is just taking the first entry, that is O(1). But if you only need the minimum once (and not repeatedly getting-and-deleting the min) it is not worth sorting the array because sorting takes O(n*log(n))

Determine if two numbers in a set equals x in nlgn [duplicate]

This question already has answers here:
Determine whether or not there exist two elements in Set S whose sum is exactly x - correct solution?
(8 answers)
Closed 7 years ago.
I do not want a solution if my own answer is incorrect because i really want to solve this on my own. What i do want is either a yes this is correct or a no this is not correct, followed by tips or suggestions that may lead to an answer without spoiling it all.
The question:
Describe a theta(nlgn)-time algorithm that, given a set S of n integers and another integer x, determines whether or not there exist two elements in S whose sum is exactly x. (Intro to Algorithms, 2.3-7)
The attempt:
First the problem doesnt state whether or not the set is sorted. I assumed it was not, and sorted it using merge sort as it is theta(nlgn) under the worst case.
Then i said okay, the only way this will still be theta(nlgn) is if i recursively split the problem into two. My approach was to start at index i=0 of our array, see what value k would i need for x=i+k and then used binary search to split the problem in half until i either found k or not. If i did not find a k such that x=i+k, then i would continue the same process for index i=2 to n-1. This would result in theta(nlgn).
The total time complexity from both sorting the array and finding k would add up to theta(nlgn) + theta(nlgn) = theta(2nlg2n) = theta(nlgn) if you remove constants.
Yes, your solution is correct. Don't forget to handle the case when a found element k has the same index i.

Find the smallest missing number in an array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Find the Smallest Integer Not in a List
I got asked this question in an interview
Given an unsorted array, find the smallest number that is missing. Assume that all numbers are postive.
input = {4,2,1,3,678,3432}
output = 5
Sorting it was my first approach. Second approach of mine is to have a boolean flag array.The second approach takes up lots of space.
Is there any other better approach than this ?
Suppose the length of the given array is N.
You can go with the boolean-flag approach, but you don't need to take numbers that are larger than N into account, since they're obviously too big to affect the answer. You could also consider a bitmap to save some space.
An alternative solution to unkulunkulu's is the following:
k := 1
Make an array of 2^k booleans, all initially set to false
For every value, V, in the array:
if V < 2^k then:
set the V'th boolean to true
Scan through the booleans, if there are any falses then the lowest one indicates the lowest missing value.
If there were no falses then increment k and go to step 2.
This will run in O(n log s) time and O(s) space, where n is the size of the input array and s is the smallest value not present. It can be made more efficient by not rechecking the lowest values on successive iterations, but that doesn't change the constraints.

Resources