Home > Back-end >  TypeError: Message=int() argument must be a string, a bytes-like object or a real number, not '
TypeError: Message=int() argument must be a string, a bytes-like object or a real number, not '

Time:09-18

So I am trying to make a random sample generator and I've got the following functions just for the sampling functions but it doesn't seem to work? (I'm trying to challenge myself by not using numpy or import random at all. However I am allowed to use import math

This is my pseudo number functions as a pre set-up incase something there is wrong:

def fakenpzeros(size):
    rows, cols = 1, size
    matrix = [([0]*cols) for i in range(rows)]
    return matrix
def pseudo_uniform(low = 0,
                   high = 1,
                   seed = 123456789,
                   size = 1):
    #generates me a number thats uniform between the high and low limits I've set
    return low   (high - low) * pseudo_uniform_good(seed = seed, size = size)
def pseudo_uniform_bad(mult = 5,
                       mod = 11,
                       seed = 1,
                       size = 1):
    U = fakenpzeros(size)
    x = (seed * mult   1) % mod
    U[0] = x / mod
    for i in range(1, size):
        x = (x * mult   1) % mod
        U[i] = x / mod
    return U
def pseudo_uniform_good(mult = 16807,
                        mod = (2 ** 31) - 1,
                        seed = 123456789,
                        size = 1):
    U = fakenpzeros(size)
    x = (seed * mult   1) % mod
    U[0] = x / mod
    for i in range(1, size):
        x = (x * mult   1) % mod
        U[i] = x / mod
    return U

Then this is my code for the random sampling

def randsamp(x):
    #Sets a seed based on the decimal point of your system clock, solves the rand.seed problem without introducing the random library
    t = time.perf_counter
    seed = int(10**9*float(str(t-int(t))[0:]))
    #makes an index of random smaples
    l = len(x)
    s = pseudo_uniform(low=0, high=l, seed=seed, size = 1)
    idx = int(s)
    return (x[idx])

I got an error of:

Message=int() argument must be a string, a bytes-like object or a real number, not 'builtin_function_or_method'

Could someone tell me what's wrong here? I've searched up this error but similar errors are like 'not list' or 'not NoneType' and none of their solutions seem to help me.

The specific line of code that is the problem is:

seed = int(10**9*float(str(t-int(t))[0:]))

CodePudding user response:

You found your problem, but I'd like to focus on the debugging process.

In response to your search comment I've searched up this error but similar errors are like 'not list' or 'not NoneType' and none of their solutions seem to help me.

When an error cites a specific function, the first thing we should search for is the docs of that function. Secondly, we need to check if our arguments conform to what it expects.

I'm see too many cases of people getting lost by doing broad internet searches, and expecting to find a close match and fix. In your case that doesn't do much good because few people make the mistake of using a method as argument to int. int(None), int([1,2,3]) or int('abc') are more common int errors.

Here's the docs for int (via ipython ?):

In [115]: int?
Init signature: int(self, /, *args, **kwargs)
Docstring:     
int([x]) -> integer
int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments
are given.  If x is a number, return x.__int__().  For floating point
numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string,
bytes, or bytearray instance representing an integer literal in the
given base.  The literal can be preceded by ' ' or '-' and be surrounded
by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.
Base 0 means to interpret the base from the string as an integer literal.
>>> int('0b100', base=0)
4
Type:           type
Subclasses:     bool, IntEnum, IntFlag, _NamedIntConstant, Handle

The key is that int expects a number or string which it can convert to an integer.

Your problem line has two uses of int

int(10**9*float(str(t-int(t))[0:]))

The outer int probably isn't the problem, since its argument is the result of some math 10**9*.... The inner int(t) is more likely the problem. So key debugging step is to examine how t was created.

  • Related