lundi 2 septembre 2019

I can't figure out how to make a proper thread-safe random number generator

I'm trying to build a thread-safe random number generator, without any luck. I looked for some solutions, but none of them worked for some reason, can you help me figure out why and show me how to do it?. Here is the struct:

struct A{
    std::thread tid;
    std::default_random_engine generator;

    float val_0;
    float val_1;

    void start(float lb, float ub){
        tid = std::thread([&lb, &ub, this](){
                generator.seed(std::random_device{}());

                val_0 = R_0(generator, lb, ub);
                val_1 = R_1(lb, ub);
        });
    }

    void wait(){
        tid.join();
    }

    float R_0(std::default_random_engine& generator, float lower_bound=0.0, float upper_bound=1.0){

        return std::uniform_real_distribution<float>{lower_bound, upper_bound}(generator);
    }

    float R_1(float lower_bound=0.0, float upper_bound=1.0){
        static thread_local std::mt19937 generator(std::random_device{}());
        return std::uniform_real_distribution<float>{lower_bound, upper_bound}(generator);
    }
};


As you can see, I tried two alternatives (R_0, R_1) but both provides garbage values. Here is an output example of 10 start() invokes:

0.502351
-8.97876e-14
0.93787
0.203829
0.0451958
0.118904
0.783515
0.155594
0.649292
-4.22577e-13




c# generate random number with trend and max and min offset

I have a function that generates a double number between 0 and 2.2:

Random random = new Random();
double value= random.NextDouble() * (2.2 - 0) + 0;

This works great but what I need is that the random value is +/- 0.2 greater or lower than the previews generated one.

For example:

If first random number is: 1.3434434, the next random number could be 1.5434434 or 1.1434434 and so on, so the numbers can have a trend going up and then could go down but the difference between the preview generated and the new one cant be greater than 0.2

Any easy way to achieve this?




How to parse data from random.org in JS?

I have a random.org system that is connected to the Laravel controller:

$random = new RandomOrgClassicClient();
$arrRandomInt = $random->generateIntegers(1, 1, $lastBet->to, false, 10, true);
$winTicket = $arrRandomInt[0];
$this->game->signature = $random->last_response['result']['signature'];
$this->game->random = json_encode($random->last_response['result']['random']);
$this->game->number = $winTicket;

How can I parse this data in the JS? I tried doing this:

<form action='https://api.random.org/verify' method='post' target=\"_blank\">
<input type='hidden' name='format' value='json' />
<input type='hidden' name='random' value='" + JSON.stringify(data.game.random) + "' />
<input type='hidden' name='signature' value='" + data.game.signature + "' />
<input class=\"check-on-random-org-btn\" type='submit' value='Check on Random.org' /></form>

Also i try change data.game.random to data.random and data.game.signature to data.signature, but every time I get a signature error on the site random.org.

How i can fix it?




Why is the sum of proportions in susceptible-infected-removed (SIR) models greater than100?

I would like to test susceptible-infected-removed (SIR) models.

I taken the core code from the Kolaczyk and Csárdi book (Section 8.5, pp. 157-158).

library(igraph)
gl <- list()
gl$er <- erdos.renyi.game(250, 1250, type=c("gnm"))
gl$ba <- barabasi.game(250, m=5, directed=FALSE)
gl$ws <- watts.strogatz.game(1, 100, 12, 0.01)

And decrese the no.sim from 100 to 10.

sim <- lapply(gl, sir, beta=0.5, gamma=1, no.sim=10) 

Then I added some commands in order to plot the characterization of an SIR process, showing how the relative proportions of those susceptible (NS, green), infective (NI, blue), and removed (NR, red) vary over time for three used graph models: Erdos-Renyi (er), Barabasi (ba), Watts-Strogatz (ws).

x.max <- max(sapply(sapply(sim, time_bins), max))

y.maxNI <- 1.05 * max(sapply(sapply(sim, function(x) median(x)[["NI"]]), max, na.rm=TRUE))

y.maxNS <- 1.05 * max(sapply(sapply(sim, function(x) median(x)[["NS"]]), max, na.rm=TRUE))

y.maxNR <- 1.05 * max(sapply(sapply(sim, function(x) median(x)[["NR"]]), max, na.rm=TRUE))

y.max <- max(y.maxNI, y.maxNS, y.maxNR)
par(mfrow=c(1,3))
#################################################################################
plot(time_bins(sim$er), median(sim$er)[["NI"]], type="l", lwd=2, col="blue", xlim=c(0, x.max), ylim=c(0, y.max), xlab="Time", ylab="Proportion of Population")

lines(time_bins(sim$er), median(sim$er)[["NS"]], type="l", lwd=2, col="green")

lines(time_bins(sim$er), median(sim$er)[["NR"]], type="l", lwd=2, col="red")
#################################################################################
plot(time_bins(sim$ba), median(sim$ba)[["NI"]], type="l", lwd=2, col="blue", xlim=c(0, x.max), ylim=c(0, y.max), xlab="Time", ylab="Proportion of Population")

lines(time_bins(sim$ba), median(sim$ba)[["NS"]], type="l", lwd=2, col="green")

lines(time_bins(sim$ba), median(sim$ba)[["NR"]], type="l", lwd=2, col="red")

#################################################################################
plot(time_bins(sim$ws), median(sim$ws)[["NI"]], type="l", lwd=2, col="blue", xlim=c(0, x.max), ylim=c(0, y.max), xlab="Time", ylab="Proportion of Population")

lines(time_bins(sim$ws), median(sim$ws)[["NS"]], type="l", lwd=2, col="green")

lines(time_bins(sim$ws), median(sim$ws)[["NR"]], type="l", lwd=2, col="red")

The results are below:

In the network-based analogue of the tradition SIR process then follows by defining the processes NS(t),NI(t), and NR(t), counting the numbers of susceptible, infective, and removed vertices at time t, respectively, in analogy to the traditional case.

But as one can see the sum of proportions NS(t)+NI(t)+NR(t) equals to 100 only for the Watts-Strogatz model.

Question. I would like to know what is a reason that NS(t)+NI(t)+NR(t) > 100 for the Erdos-Renyi (er), Barabasi (ba) model?

The error in my code or in models side?




How could I fill my second list with zero and one according to the conditions below?

I have a list contains multiple lists. It's full of random numbers between 0 and 1. I need to create another list with the same size of the first one, but if the random numbers less or equal to 0.75, I need them equal to zero and the more than 0.75 will be one. I always get a list full of zeros, where is my fault?

This is below my try:

import random

y = [[random.uniform(0,1) for i in range(10)]for j in range(10)]
x = [[0 for i in range(len(y[0]))]for j in range(len(y))]

for i in range(len(y)):
    for j in range(len(y[0])):
        if y[i][j] <= 0.75:
            x[i][j] == 0
        else:
            x[i][j] == 1
print(x)




dimanche 1 septembre 2019

Why does the Python random.random() give a different value if the previously generated value is explicitly set as the new seed?

I have read that the random module in Python uses the previously generated value as the seed except for the first time where it uses the system time. If this is true, why don't I get the same value when I explicitly set the previously generated value as the new seed like this:

random.seed(random.randint(1, 100))

The same doesn't work for the random.random() method either.

>>> import random
>>> random.seed(20)
>>> random.randint(1,100)
93
>>> random.randint(1,100)
88
>>> random.seed(20)
>>> random.randint(1,100)
93
>>> random.randint(1,100)
88
>>> random.seed(20)
>>> random.seed(random.randint(1,100))
>>> random.randint(1,100)
64

Why didn't the last randint() call not give 88?

Thanks in Advance!




Need help regarding random password generation possibilities

I had generated a random password from a website which unfortunately has put me into trouble.

The password contains 26 characters which includes alphabets, numbers and special characters. My trouble started when it was found that among the 26 characters there was a 4 letter Malayalam word (spelled in English) which is interpreted to be abusive, followed by an exclamation mark and then again followed by 3 alphabets which unfortunately consists of the initials of the person to whom I had sent the password.

How can I make others believe that this can be auto generated using a random password/string generator algorithm?

I request those people who are acquainted with such algorithms to please help me in this regard. Those who can help me may please reply at the earliest.