コード例 #1
0
def metropolis_step_removal(X, move_index=None):
    '''For a given state vector X, apply MC Metropolis move and update
        bookkeeping associated with counting grid'''
    global accepted, rejected, counting_grid

    # Original point
    if move_index == None:
        index = fastrand.pcg32bounded(N)
    else:
        index = move_index

    point_old = X[index].copy()
    counting_index_old = (
        int(point_old[1] * grid_marks_y / Ly),
        int(point_old[0] * grid_marks_x / Lx)
    )  # the index that the particle appears in in the counting grid

    # Propose new move
    point_new = [0, 0]
    point_new[0] = (point_old[0] + step * (fastrand.pcg32() / 2147483647)) % Lx
    point_new[1] = (point_old[1] + step * (fastrand.pcg32() / 2147483647)) % Ly
    counting_index_new = (int(point_new[1] * grid_marks_y / Ly),
                          int(point_new[0] * grid_marks_x / Lx))

    # Use counting grid to check for nearest neighbours
    neighbour_indicies = [
        neighbour for neighbours in extract_3X5(counting_grid,
                                                counting_index_new).flatten()
        for neighbour in neighbours
    ]

    # Determine whether overlap occurs
    # Here is the difference from old step. Move accepted if it's further away
    for point_index in neighbour_indicies:
        if torus_dist(point_new,
                      X[point_index]) < 2 * r and point_index != index:
            rejected += 1
            return X

    # Else accept move
    accepted += 1
    X[index] = point_new

    # Determine whether grid counts need to be changed
    if counting_index_old != counting_index_new:
        counting_grid[counting_index_old].remove(index)
        counting_grid[counting_index_new].append(index)

    return X
コード例 #2
0
import fastrand
import time
import numpy as np
import matplotlib.pyplot as plt

# Time tests

# Fastrand32 float
t0 = time.time()
for i in range(10000000):
    my_random_float = fastrand.pcg32() / 2147483647
print('fastrand32 float: ', time.time() - t0, 's')

# Numpy float
t0 = time.time()
for i in range(10000000):
    my_random_float = 2 * (np.random.rand() - 0.5)
print('numpy float: ', time.time() - t0, 's')

# Fastrand int
t0 = time.time()
for i in range(10000000):
    my_random_int = fastrand.pcg32bounded(250)
print('fastrand int: ', time.time() - t0, 's')

# Numpy int
t0 = time.time()
for i in range(10000000):
    my_random_int = np.random.randint(0, 250)
print('numpy int: ', time.time() - t0, 's')
コード例 #3
0
 def reseed():
     # seed from system and flush initial output
     fastrand.pcg32_seed(random.getrandbits(63))
     fastrand.pcg32()
     fastrand.pcg32()
コード例 #4
0
ファイル: generate_xml.py プロジェクト: akx/so63265336
import itertools
import fastrand

TARGET_SIZE = 1024 * 1024 * 1000

with tqdm.tqdm(total=TARGET_SIZE) as p:
    with io.StringIO() as f:
        print("""<?xml version="1.0" encoding="utf-8" ?>""", file=f)
        print("<badges>", file=f)
        for id in itertools.count():
            p.n = f.tell()
            p.update(n=0)
            if f.tell() >= TARGET_SIZE:
                break
            userid = fastrand.pcg32bounded(100000) + 1
            name = "".join(
                (string.ascii_lowercase
                 )[fastrand.pcg32bounded(len(string.ascii_lowercase))]
                for x in range(5, 5 + fastrand.pcg32bounded(8)))
            date = datetime.datetime(2020, 1, 1) + datetime.timedelta(
                seconds=fastrand.pcg32bounded(86400 * 150))
            cls = fastrand.pcg32bounded(10) + 1
            tagbased = fastrand.pcg32() & 1
            print(
                f'<row Id="{id}" UserId="{userid}" Name="{name}" Date="{date.isoformat()}" Class="{cls}" TagBased="{str(tagbased).title()}" />',
                file=f,
            )
        print("</badges>", file=f)
        with open("badges.xml", "w") as ff:
            ff.write(f.getvalue())
コード例 #5
0
from multiprocessing import Pool
from tqdm import tqdm

PROCESSES = 6

import fastrand

r_number = lambda: fastrand.pcg32() / int(2**32)


def fastmap(function, iterable, display_progress=False):
    with Pool(processes=PROCESSES) as p:
        max_ = len(iterable)
        if display_progress:
            with tqdm(total=max_) as pbar:
                result = list()
                for res in p.imap(func=function, iterable=iterable):
                    pbar.update()
                    result.append(res)
                return result
        else:
            return list(p.imap(func=function, iterable=iterable))


#test
if __name__ == '__main__':

    def double(x):
        return x * 2

    print(list(fastmap(double, range(10))))