generation of numbers that are x percent likely to be a duplicate - python-2.7

i am generating random numbers currently like this :
rand.sample(range(1, network_ip_node_size))
this generates a random number from 1 to x and is working fine
however what i am trying to do is say i have a list (list_of_nodes) for example, i want to generate a random number, but say have a probability of 20% of being a number already contained within the list_of_nodes
what is the most effecient way of achieving this ?

Related

How to Generate distinct large random number/keys in c++

generate large random number in c++ which is closely near to pow(2, 64)-1.
Explain that problem that let say we should generate n random numbers in which mostly is big and stored them in a array.
The question really needs more clarification. What is 'closely near'? Are you trying to generate weighted random values or is (2^64)-1 the maximum value?
For the sake of providing an answer I assume the latter and that your random value is always positive:
Generation could be done using the standard library:
How to generate 64 bit random numbers?
You need to use uint64_t or similar to store the number. Any smaller and the data could overflow the int bound.
https://en.wikipedia.org/wiki/Integer_overflow

How to Generate n digit random numbers? [duplicate]

This question already has answers here:
Getting big random numbers in C/C++
(6 answers)
Closed 6 years ago.
I'm trying to generate n digit random numbers. I can generate a random number, but here the case is to generate a n digit number.
I tried doing it by storing the random numbers in an array but I need it in a long long format and not an array.
There are two things you need to do:
Work out how to output random numbers within a given range
Work out what range you need in order to get only 10-digit numbers
Part (1) is actually a bit tricky, if you want to ensure every number in your range is equally likely to occur. Fortunately, the standard library in C++11 onwards comes with a facility called uniform_int_distribution which does the required calculations for you:
// Create and seed the random number generator
auto gen = std::mt19937{std::random_device{}()};
// Create our desired distribution
auto dist = std::uniform_int_distribution<int_type>{lower, upper};
// Get numbers
std::cout << dist(gen) << "\n";
For part (2), we need to work out what lower and upper should be above. That's actually pretty easy: the lowest 10-digit number is 1,000,000,000 (one billion), and the highest is 9,999,999,999 (one less than 10 billion). We can put these numbers straight in to C++
constexpr auto lower = 1'000'000'000;
constexpr auto upper = 9'999'999'999;
(Note you'll need a C++14 compiler to use ' as a digit separator).
Now, there's one last problem: on a typical system, lower and upper above will be different types, because lower will fit into an int but upper will not. So we need to make sure that our output uses the larger of the two types. A good way to do this is to use a type alias and decltype:
using int_type = decltype(upper);
This says we are declaring a new type name int_type which is an alias for the type of upper.
Put these together and you'll have a routine that will output 10 digit numbers on any system that uses C++11.

map random numbers

for(int i=0;i<100;i++)
for(int j=0;i<6;j++)
{
cout<<rand()%6<<"," // Store these numbers in a map
}
cout<<endl;
Say, suppose I store these random numbers in the inner for loop in a map<int,myRandomNumbers>
In some random game, game maker also created a similar call to rand()%6 to get all 6 numbers. Are these 6 numbers having any slightest chance to be fully or partially the same as one of myRandomNumbers ?
Well, you can calculate it, assuming rand() gives a completely uniform distribution (it doesn't, but we'll assume it does anyway). If you generate 6 random numbers in the range [0, 5], the probability that another set of 6 random numbers generated from the same range are exactly the same is (1/6)^6 ~ 2.14e-5. You can use the binomial distribution to calculate the probability that they will be partially similar, that is, match in n places for n in [0, 6].
Unless you seed the rand() function by calling srand(), there's a good chance that you will get exactly the same numbers (assuming the random numbers are created by different processes, that is).

Unbalanced random number generator

I have to pick an element from an ascending array. Smaller elements are considered better. So if I pick an element from the beginning of the array it's considered a better choice. But at the same time I don't want the choice to be deterministic and always the same element. So I'm looking for
a random numbers generator that produces numbers in range [0, n], but
the smaller the number is, the more chance of it being produced.
This came to my mind:
num = n;
while(/*the more iteration the more chance for smaller numbers*/)
num = rand()%num;
I was wondering if anyone had a better solution.
I did look at some similar questions but they have details about random number generation generally. I'm looking for a solution to this specific type of random number generation, either an algorithm or a library that provides it.
Generate a Random number, say x, between [0,n) and then generate another Random floating point number, say y, between [0,1]. Then raise x to the power of y and use floor function, you'll get your number.
int cust(int n)
{
int x;
double y, temp;
x = rand() % n;
y = (double)rand()/(double)RAND_MAX;
temp = pow((double) x, y);
temp = floor(temp);
return (int)temp;
}
Update: Here are some sample results of calling the above function 10 times, with n = 10, 20 and 30.
2 5 1 0 1 0 1 4 1 0
1 2 4 1 1 2 3 5 17 6
1 19 2 1 2 20 5 1 6 6
Simple ad-hoc approach that came to my mind is to use standard random generators, but duplicate indices. So in the array:
0, 0, 0, 1, 1, 2, 3
odds are good that smaller element will be taken.
I dont' know exactly what do you need. You can also define your own distribution or maybe use some random number generation libraries. But suggested approach is simple and easy to configure.
UPDATE2: You don't have to generate array explicitly. For array of size 1000, you can generate random number from interval: [0,1000000] and then configure your own distribution of selected values: say, intervals of length 1200 for smaller values (0-500) and intervals of length 800 for larger (500-1000). The main point that this way you can easily configure the probability and you don't have to re-implement random number generator.
Use an appropriate random distribution, e.g. the rounded results of an exponential distribution. Pick a distribution that fits your needs, document the distribution you used, and find a nice implementation. If code under the GNU Public License is an option, use the excellent GNU Scientific Library (GSL), or try Boost.Random.
Two tools will solve many random distribution needs
1) A uniform random number generator which you have
2) and a function which maps uniform values onto your target distribution
I've gotta head to the city now, but I'll make note to write up a couple of examples with a drawing when I get back.
There are some worthwhile methods and ideas discussed in this related question (more about generating a normal pseudo random number)

random complex number

i need algorithm for generate random complex number please help i know how generate random number but random complex number confuse me
I would simply generate two random numbers and use one for the real part and one for the imaginary part.
Generate 2 random numbers (x, y) (use the built-in rand/rnd/random class from your environment's libraries), where x is the real part and y is the imaginary part.
Create a complex number class (with a constructor that takes a real and imaginary parameter)
Use the 2 random numbers from step 1 to create a complex number, x + i y
1.Generate 2 vector of numbers say one is real_vector and another is imaginary_vector of size say MAX_SIZE to be generated randomly with differrent seeds.
2.Random shuffle the numbers in vectors(real_vector+imaginary_vector) using any distribution let us say use of std::random_shuffle(uniform distribution).
3.randomly generate a index and apply modulo operator for MAX_SIZE and select index from first array that will provide an real part of ur random number.
4.use step 3 to get imaginary part of your random number.
5.Create a complex number using number got from step 3 and step 4 and store in a container.
6.go to step 3 and check if you want any more complex number;if no then break;