mardi 3 mars 2020

Godot Random Generate Terrain using NoiseMap (C#)

Hi nearly I start intrestring a procedural generated map because I would add this into my project so first I created new project to test code from internet I searched and searched finally I found a code with NoiseMap who work partly because this generates procedural stuff but with one texture and here is my question someone could tell what there work wrong. btw sorry for my bad english

using Godot;
using System;

public class randomMap : TileMap
{
OpenSimplexNoise osn;

public void create_noise(int x,int y,int width,int height)
{
Random rnd = new Random();

int randomtexture = rnd.Next(0, 4);


for (int x_pos = 0; x_pos < width; x_pos++)
{
    for (int y_pos = 0; y_pos < height; y_pos++)
    {
        SetCell(x_pos + x, y_pos + y, (int)osn.GetNoise2d(x_pos+x, y_pos+y)*2+2);
    }
 }
}

public override void _Ready()
{

Random rnd = new Random();
osn = new OpenSimplexNoise();
int randomInt = rnd.Next(0, 100);
GD.Print(randomInt);
osn.Seed = randomInt;
osn.Octaves = 4;
osn.Period = 15;
osn.Lacunarity = 1.5f;
osn.Persistence = 0.75f;

create_noise(100, 100, 100, 100);
}


  public override void _Process(float delta)
  {

  Camera2D camera = GetNode<Camera2D>("Camera2D");
  camera.Position += (GetGlobalMousePosition() - camera.Position).Normalized() * 1000 * delta;
  }
}



Using JavaScript to get random 2-digit numbers in a specific range where both the 1st and 2nd digits are constrained

I've got a bit of an interesting mathematical puzzle to solve.

I'm writing a test to verify that values outside of a range of a field in a window are not allowed to be input. For the most part this is extremely easy.

I'm using a function like this:

function getRanInt(min, max)
{
    let min = Math.ceil(min);
    let max = Math.floor(max);
    return Math.floor(Math.random() * (max - min) + min);
}

to set the range of "erroneous" values I want to test.

However, there is one field which throws a wrench in this method. One field has constraints on each digit. So the first digit can only be between 0-7 and the second digit can only be between 0-3.

My first thought was to simply concatenate two numbers together that I could separately constrain like this:

let m1_1;
let m1_2;

m1_1 = getRanInt(8,9);
m1_2 = getRanInt(0,7);

m1 = m1_1.toString() + m1_2.toString();
m1 = Number(m1);

until I thought about it for half a second and realized that this would only get me erroneous values > 83 but leave out an entire array of possible erroneous values like 24, 36, 18 etc...

I've been wracking my brain to try and come up with a solution that isn't massively convoluted but I've been drawing a blank. I thought I'd turn to you fine braniacs here and see if you could maybe help unstick my gears.

ADDITIONAL: If anyone's interested, the tool tip for the field in question explicitly says this:

2 Digits:
1st = 0 - 7
2nd = 0 - 3



Random Number Sequence in C++ [duplicate]

I am trying to generate a random number sequence. I have the following function

//Returns a random number between lb and hb
double rand_num(double lb, double hb){

    mt19937 gen(time(nullptr));

    uniform_real_distribution<> dis(lb, hb);

    return dis(gen);
}

If I call this function for the same bounds over and over again, I get a different value so it seems to work. However, when I put it in this loop to generate the sequence it just returns the same value

for(int i = 0; i<10000; i++){
    myfile << i << " " << rand_num(0,1) << endl;
}

I do acknowledged that I am getting the warning random number generator seeded with a disallowed source of seed, but I don't understand this since time(nullptr) is different every time I call it. If anyone could help me understand what is going on and/or tell me how successfully generate the sequence, that would be much appreciated. Thanks in advanced!

Edit: Now I know that "if you run your function in a loop they will be executed faster than the time resolution interval so all the seeds are the same and therefore the numbers." However, I still don't know how to generate a sequence instead of a single random number.




Get indexes for Subsample of list of lists

I have several lists of data in python:

a = [2,45,1,3]
b = [4,6,3,6,7,1,37,48,19]
c = [45,122]
total = [a,b,c]

I want to get n random indexes from them:

n = 7
# some code
result = [[1,3], [2,6,8], [0,1]]

This mean I get first and fhird from first list, 2,6,8 from second and 0th and 1th from third list.

So my idea - generate random indexes:

n = 7
total_len = sum([len(el) for el in total])
inds = random.sample(range(total_length), n))

But how then get such indexes? I think about np.cumsum() and shift indixes after that but can't find elegant solution...


P.S. Actually, I need to use it for loading data from a several csv files using skiprow option. So my idea - get indexes for every file, and this let me load only necessary rows from every file. So my real task: i have several csv files of different length and need to get n random rows from them. My idea:

lengths = my_func_to_get_lengths_for_every_csv(paths) # list of lengths
# generate random subsamle of indexes
skip = ...
for ind, fil in enumerate(files):
    pd.read_csv(fil, skiprows=skip[ind])



Randomizing a list of zeros and ones with constraints

I'm currently trying to randomize a list of 0s and 1s which should give a random order of zeros and ones with the following constraints: 1) 1/3 of the items have to be 1s (respectively 2/3 are 0s) 2) no more than two 1s should occur consecutively 3) no more than four zeros should occur consecutively

I have worked on an option, but it did not exactly turn out to be what I need. Here's my option:

for prevItem, nextItem in enumerate(WordV[:-1]):
            if nextItem  == WordV[prevItem+1] and WordV[prevItem+1] == WordV[prevItem+2] and nextItem ==1: 
                WordV[prevItem+2] = 0
            if nextItem  == WordV[prevItem+1] and WordV[prevItem+1] == WordV[prevItem+2] and WordV[prevItem+2] == WordV[prevItem+3] and WordV[prevItem+3] == WordV[prevItem+4] and nextItem == 0: 
                WordV[prevItem+2] = 1

#check the number of ones & zeros
print(WordV)
ones= WordV.count(1)
zeros= WordV.count(0)
print(ones, zeros)

Currently, the number of ones and zeros does not add up to a proportion of 1/3 to 2/3 because the constraints replace numbers. The WordV list is a list containing 24 ones and 48 zeros that is shuffled randomly (with random.shuffle(WordV)).

I'm pretty sure there is a smarter (and more correct) way to integrate the constraints into the code. Any help would be tremendously appreciated!




Randomizing an array and storing value to external file? [duplicate]

I am randomizing an array of strings and writing it to an external file in the system. I am also reading the contents of text file before I write to it so that the no duplicates are found. If there is a duplicate, I am recalling the function to generate a random string. The problem lies here: (a) Once the stack is filled, I get an error message saying StackOverflowException: The requested operation caused a stack overflow. (b) Once the stack is filled, the shuffle function completes the function to generate random string very slow

Is there a better way to achieve what I am doing? Right now it is only 5 array of strings. It could be a thousand later. Should I use List instead of Array?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class GenerateRandomNumber : MonoBehaviour
{
    private string[] animals = new string[5] {"Pig", "Dog", "Camel", "Giraffe", "Elephant"};
    public string GetAnimal;
    public string word;
    string path;
    void Start()
    {
       path = "Assets/StreamingAssets/ID.txt";
    }

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.R))
        {
            ShuffleAnimals();
        }
    }

        public void ShuffleAnimals()
      {
           GetAnimal = animals[Random.Range(0, animals.Length)];

           word = File.ReadAllText(path);

           if(word.Contains(GetAnimal))
           {
               ShuffleAnimals();
           }

           else

           File.AppendAllText(path, GetAnimal + System.Environment.NewLine);
      }
}



Run random order test class in Java project with maven

I have a java project and I have to launch the methods of a test class in random order. Is there a way to do that with Maven?

I tried with surefire and runOrder, but the configuration works with a test suite, in fact it launches the classes in random order but not the methods within them.