jeudi 2 novembre 2023

how to create a random sequence

I made an application in Android Studio and I'm confused about coding float sequentially from ordinal numbers 1 to 10 or more can you provide a code example?

I code sequentially in Android studio Can someone help me code it?




mercredi 1 novembre 2023

Is "srand(time(0) * int_value))" a good way to get different values for each execution?

I want to get random float values between 0 and 10 for each execution, and if i don't use **srand(time(0)), **my random values always same in each execution. However, if i use this, i get different values but it changes like 0.01 between two consecutive executions.

So my question is this:

If I use srand(time(0) * 1000), I get different random numbers for each execution. Is this a proper way to use like this, or will there be an overflow because of seed limits or something like that for a specific times?

Here is a simple function to get different float values between 0 and 10

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {

    srand(time(0) * 1000);

    int i;
    for (i = 0; i < 10; i++) {
        float randomValue = ((float)rand() / RAND_MAX) * 10;
        printf("Random float value %d: %.2f\n", i + 1, randomValue);
    }

    return 0;
}



Why is torch.randn not been reproducible with all seeding?

Title says it all, here is my issue:

I want torch.randn(1) to be reproducible.

I am using CUDA and have tried the following to seed it.

seed=42
torch.manual_seed(seed=seed)
torch.cuda.manual_seed_all(seed=seed)
np.random.seed(seed=seed)
torch.backends.cudnn.deterministic = True
torch.use_deterministic_algorithms(True)
torch.backends.cudnn.benchmark = False 

torch.randn(1)

I was expecting a constant value as an output.




Making different and distinguishable random colors in c#

I want to make up to 100 different and distinguishable random colors in my winforms project. The method will executes in a loop as my desired times. I know how to make just random colors, but is there any Idea how to make different and distinguishable random colors, that a user by looking it could easily see the differences and recognize them?

My code:

private System.Drawing.Color RandomColor()
    {
         Random rand = new Random();
         int max = byte.MaxValue + 1; // 256
         int r = rand.Next(max);
         int g = rand.Next(max);
         int b = rand.Next(max);
         System.Drawing.Color color = System.Drawing.Color.FromArgb(r, g, b);
         return color;
   }

   for(int i=0; i<100 or lesss; i++)
   {
      var randomColor = RandomColor();
      //I use randomColor to draw a thin line on my form.
   }



Did Java get a new feature to supplant `ThreadLocalRandom`?

I vaguely recall reading some mentions that ThreadLocalRandom has been supplanted in modern Java by some new random number generators.

Did Java add a new feature for housing a random number generator per thread?




mardi 31 octobre 2023

Generating Random numbers following lognormal distribution [closed]

I am generating pseudo random numbers following lognormal distribution using following python but the numbers are not validating against the data I already have. I have mean and standard deviation of the lognormal distribution (Mean=0.198, std=0.0963). Need some help on it. Thanks. Code is as following

import numpy as np

mean = float(input("Enter the mean of the log-normal distribution: "))
std_dev = float(input("Enter the standard deviation of the log-normal distribution: "))

# Generate and display 365 random numbers following the log-normal distribution
for _ in range(365):
    random_number = np.random.lognormal(mean, std_dev)
    print(random_number)



generate random data when they are potentially correlated using copula

I have a task to evaluate companies' pro-environment performance, I have some sample indicators: ['Emissions', 'Energy_Use', 'Waste', 'Water_Use', 'Recycling',......]

some of the indicators may be correlated, for example, if a company has a large number of emissions, then the energe_use will not be low.

I know copula can deal with the dependence between random variables, my aim is to generate random numbers for each indicator (let's say 100 companies) that may be close to realisaion. But I only know the basic way (np.random) to generate isolated indicator numbers, however, indicators may be correlated.