kandi background
kandi background
Explore Kits
kandi background
Explore Kits
kandi background
Explore Kits
kandi background
Explore Kits
Explore all Generator open source software, libraries, packages, source code, cloud functions and APIs.

Explore Related Topics

Popular New Releases in Generator

v7.8.1

v6.0.0-beta released

AutoValue 1.9

GPRG v1.2.0

v0.22.15

generator-jhipster

v7.8.1

openapi-generator

v6.0.0-beta released

auto

AutoValue 1.9

github-profile-readme-generator

GPRG v1.2.0

typedoc

v0.22.15

Popular Libraries in Generator

Trending New libraries in Generator

Top Authors in Generator

1

30 Libraries

337

2

29 Libraries

359

3

27 Libraries

116

4

23 Libraries

848

5

18 Libraries

46

6

16 Libraries

13918

7

13 Libraries

502

8

11 Libraries

20896

9

10 Libraries

750

10

10 Libraries

61

1

30 Libraries

337

2

29 Libraries

359

3

27 Libraries

116

4

23 Libraries

848

5

18 Libraries

46

6

16 Libraries

13918

7

13 Libraries

502

8

11 Libraries

20896

9

10 Libraries

750

10

10 Libraries

61

Trending Kits in Generator

Trending Discussions on Generator

    How can I make an object with an interface like a random number generator, but that actually generates a specified sequence?
    Why is `np.sum(range(N))` very slow?
    How would you implement a lazy "range factory" for C++20 ranges that just calls a generator function?
    Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/tokenize' is not defined by "exports" in the package.json of a module in node_modules
    Can you compress angular image assets on build?
    Convert a bytes iterable to an iterable of str, where each value is a line
    Problem Updating to .Net 6 - Encrypting String
    Ensure that an argument can be iterated twice
    Proper way to DI NSwag auto-generated client
    How to alias generic types for decorators

QUESTION

How can I make an object with an interface like a random number generator, but that actually generates a specified sequence?

Asked 2022-Mar-31 at 13:47

I'd like to construct an object that works like a random number generator, but generates numbers in a specified sequence.

1# a random number generator
2rng = lambda : np.random.randint(2,20)//2
3
4# a non-random number generator
5def nrng():
6    numbers = np.arange(1,10.5,0.5)
7    for i in range(len(numbers)):
8        yield numbers[i]
9
10for j in range(10):
11    print('random number', rng())
12    print('non-random number', nrng())
13

The issue with the code above that I cannot call nrng in the last line because it is a generator. I know that the most straightforward way to rewrite the code above is to simply loop over the non-random numbers instead of defining the generator. I would prefer getting the example above to work because I am working with a large chunk of code that include a function that accepts a random number generator as an argument, and I would like to add the functionality to pass non-random number sequences without rewriting the entire code.

EDIT: I see some confusion in the comments. I am aware that python's random number generators generate pseudo-random numbers. This post is about replacing a pseudo-random-number generator by a number generator that generates numbers from a non-random, user-specified sequence (e.g., a generator that generates the number sequence 1,1,2,2,1,0,1 if I want it to).

ANSWER

Answered 2022-Mar-29 at 00:47

You can call next() with a generator or iterator as an argument to withdraw exactly one element from it. Saving the generator to a variable beforehand allows you to do this multiple times.

copy icondownload icon

1# a random number generator
2rng = lambda : np.random.randint(2,20)//2
3
4# a non-random number generator
5def nrng():
6    numbers = np.arange(1,10.5,0.5)
7    for i in range(len(numbers)):
8        yield numbers[i]
9
10for j in range(10):
11    print('random number', rng())
12    print('non-random number', nrng())
13# make it a generator
14def _rng():
15    while True:
16        yield np.random.randint(2,20)//2
17
18# a non-random number generator
19def _nrng():
20    numbers = np.arange(1,10.5,0.5)
21    for i in range(len(numbers)):
22        yield numbers[i]
23
24rng = _rng()
25nrng = _nrng()
26for j in range(10):
27    print('random number', next(rng))
28    print('non-random number', next(nrng))
29

Source https://stackoverflow.com/questions/71655179

Community Discussions contain sources that include Stack Exchange Network

    How can I make an object with an interface like a random number generator, but that actually generates a specified sequence?
    Why is `np.sum(range(N))` very slow?
    How would you implement a lazy "range factory" for C++20 ranges that just calls a generator function?
    Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/tokenize' is not defined by "exports" in the package.json of a module in node_modules
    Can you compress angular image assets on build?
    Convert a bytes iterable to an iterable of str, where each value is a line
    Problem Updating to .Net 6 - Encrypting String
    Ensure that an argument can be iterated twice
    Proper way to DI NSwag auto-generated client
    How to alias generic types for decorators

QUESTION

How can I make an object with an interface like a random number generator, but that actually generates a specified sequence?

Asked 2022-Mar-31 at 13:47

I'd like to construct an object that works like a random number generator, but generates numbers in a specified sequence.

1# a random number generator
2rng = lambda : np.random.randint(2,20)//2
3
4# a non-random number generator
5def nrng():
6    numbers = np.arange(1,10.5,0.5)
7    for i in range(len(numbers)):
8        yield numbers[i]
9
10for j in range(10):
11    print('random number', rng())
12    print('non-random number', nrng())
13

The issue with the code above that I cannot call nrng in the last line because it is a generator. I know that the most straightforward way to rewrite the code above is to simply loop over the non-random numbers instead of defining the generator. I would prefer getting the example above to work because I am working with a large chunk of code that include a function that accepts a random number generator as an argument, and I would like to add the functionality to pass non-random number sequences without rewriting the entire code.

EDIT: I see some confusion in the comments. I am aware that python's random number generators generate pseudo-random numbers. This post is about replacing a pseudo-random-number generator by a number generator that generates numbers from a non-random, user-specified sequence (e.g., a generator that generates the number sequence 1,1,2,2,1,0,1 if I want it to).

ANSWER

Answered 2022-Mar-29 at 00:47

You can call next() with a generator or iterator as an argument to withdraw exactly one element from it. Saving the generator to a variable beforehand allows you to do this multiple times.

copy icondownload icon

1# a random number generator
2rng = lambda : np.random.randint(2,20)//2
3
4# a non-random number generator
5def nrng():
6    numbers = np.arange(1,10.5,0.5)
7    for i in range(len(numbers)):
8        yield numbers[i]
9
10for j in range(10):
11    print('random number', rng())
12    print('non-random number', nrng())
13# make it a generator
14def _rng():
15    while True:
16        yield np.random.randint(2,20)//2
17
18# a non-random number generator
19def _nrng():
20    numbers = np.arange(1,10.5,0.5)
21    for i in range(len(numbers)):
22        yield numbers[i]
23
24rng = _rng()
25nrng = _nrng()
26for j in range(10):
27    print('random number', next(rng))
28    print('non-random number', next(nrng))
29

Source https://stackoverflow.com/questions/71655179