Skip to content

seed(n) variable function#28

Open
g0mb4 wants to merge 9 commits intoCFDEMproject:masterfrom
g0mb4:random-seed
Open

seed(n) variable function#28
g0mb4 wants to merge 9 commits intoCFDEMproject:masterfrom
g0mb4:random-seed

Conversation

@g0mb4
Copy link
Contributor

@g0mb4 g0mb4 commented Mar 27, 2023

variable    p1  equal   seed(0)
variable    p2  equal   seed(200)

The seed(n) function generates a prime number between 10000 and INT_MAX (2147483647). The output of this function can be used to be the seed of particletemplate, particledistribution, etc.

If the argument is 0, a random prime number is generated, else the n-th greater, than 10000 prime (n = 1 -> 10007, n = 2 -> 10009, etc) is the output.

Copy link
Contributor

@Azrael3000 Azrael3000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will not merge this in the current state, this needs reworking.

// used in Variable::random_seed()
if(me == 0){
// NOTE: better random generator for random_seed()?
srand(time(NULL));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause random numbers to be truly random, which is something you don't want as it destroys repeatability.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my point. If I want to repeat my simulation, I can use a predefined seed, but if I want to repeat an "experiment" with "random" particle positions, I can use randomseed().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could create a list of, say 500, prime numbers given an initial seed. If someone provides 0, you get the current behavior, and for any other number you get a list of semi-random numbers that is repeatable. Then every call to randomseed simply increments the location in the pre-compiled list.
You would also be able to add additional checks that make sure each prime number is unique.

src/variable.cpp Outdated
static const int SEED_RANGE = (LAST_PRIME - FIRTS_PRIME) + 1;

if(me == 0){
while(!MathExtraLiggghts::isPrime(seed)){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This algorithm is really bad in terms of performance. Using lookup tables would be much better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I'm afraid a list would be too big to hold it in the memory (105'097'397 numbers). I'll try to come up with a faster solution.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use the fact that all prime numbers greater than 3, are of the form 6k-1 or 6k+1.
Randomly taking k from an initial value of 20600, you get a valid prime about 30% of the time. But you do have to check if two different numbers are prime.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'll look into that.

doc/variable.txt Outdated
random(x,y,z), normal(x,y,z), ceil(x), floor(x), round(x)
ramp(x,y), stagger(x,y), logfreq(x,y,z), stride(x,y,z), vdisplace(x,y), swiggle(x,y,z), cwiggle(x,y,z)
ramp(x,y), stagger(x,y), logfreq(x,y,z), stride(x,y,z), vdisplace(x,y), swiggle(x,y,z), cwiggle(x,y,z),
randomseed(0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having an unused argument is not good practice. Rewrite the interface instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, but the parser for variable equal expects at least 1 argument for functions, if I understand the code rigth.

@g0mb4 g0mb4 changed the title randomseed() variable function seed(n) variable function May 3, 2023
@g0mb4 g0mb4 requested a review from Azrael3000 May 3, 2023 13:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants