What I’m trying to do:
generate a number randomly in the given range, but what would be the best/fast way to check it’s uniqueness. What I’ve tried and what’s not working:
Code Sample:
# this is a formatted code snippet.
# paste your code between ```
There are lots of python functions that can do this for you which can be found by googling python random function. You can use these directly in Anvil (some functions may be server code only)
As for checking the uniqueness, that seems contradictory to the idea of something being random. If instead you wanted a unique number you should search for that instead.
It is difficult to help you more with this given that there is not a lot to go on.
Might I suggest reading this post about how to ask a good question and then give the community a little more information to help you.
I am looking for something like the order number on an e-commerce website. It is not sequential at the same time it is unique, hence random and unique.
So I think what your outline should be is something like:
Create your Random Number
Attempt to retrieve that order number from your Data Table, this should fail!
If it correctly fails, your order number is not used, if it incorrectly finds a used order number, then this code should be running in a loop that tries again until it finds a non-used order number.
There are many built into python Random functions as @rickhurlbatt said, and within the first few pages of the docs for data tables you will find how to search a data table for a row using something like an order number, (Which again, should return None, since you want an unused one)
Bear in mind that row ids are themselves unique. If you want something less predictable from that, you could “scramble” it in some fashion, e.g., through a hash function.
Along the lines of what @p.colbert is talking about row ID’s…
I wanted to point out also, even though this isn’t what the OP asked for; there is nothing inherently wrong with non-random (or non-pseudorandom) commerce platforms (‘e’ or otherwise) from using sequential order numbers.
However many systems do use similar methods to what I described above to double check that an existing order ID is not duplicated.
If you are worried about the embarrassing vanity of initial customers having a low order number for your platform, simply pick some arbitrarily high number as a starting point.
Finding the highest number in a column using modern database architecture is quite easy, you sort the column as descending order, and then only take the first result.
Yeah, I guess that is a common pain point from our developers point of view regarding exploits, from my personal experience its hard enough to get a customer to actually give you their order number at all.
Customer: “Why don’t you know who I am?? It’s Jim!, that guy who bought that one thing that one time 3 months ago!” … "mumble mumble, horrible customer service…"
Also, the same is true for a bad-actor in reverse, with sequential numbers if “the wrong person” has an order time or date outside of the range of when a sequence of numbers was issued, you know there is something invalid about the information they have. Which can instantly be validated by even an abnormally low-skilled customer service employee.