r/learnpython • u/[deleted] • Jun 14 '24
I found out that random generator are not truly random, they rely on a seed and equation if that is so, how sets gets ordered random?
no matter who i ask they always say, that sets are random i did a quick google search to find out this:
order in a set, elements are not arranged in a random order but in ascending order of the hash value modulo the length of the list. is it true? if that is so why not give them index functionality too?
8
u/supercoach Jun 14 '24 edited Jun 14 '24
Sets aren't random. In the past, a number of data types didn't have order. This didn't mean they were random, however they weren't guaranteed to be displayed in the same order they were stored in. I suspect this is what people were trying to tell you.
edit: too many howevers
5
u/xiongchiamiov xiongchiamiov Jun 14 '24
What they meant was that the order in sets is effectively random to you, so don't rely on it.
3
u/mopslik Jun 14 '24
Often, the things you do with a set (check for inclusion, intersection, etc) don't require the overhead that would result from maintaining order. If you want an ordered set, try this package.
3
u/notacanuckskibum Jun 14 '24
You are over thinking it. Sets are designed to act like sets in mathematics. Sets in mathematics are about membership, but not order.
Use sets when sets are the natural construct. Use something else (probably lists) when order I’d important.
Stop worrying about how things are stored, libraries are provided so that you don’t have to think about storage and retrieval.
1
u/FerricDonkey Jun 14 '24
Stop worrying about how things are stored, libraries are provided so that you don’t have to think about storage and retrieval.
I'd qualify this. The art least a general idea of the how is important, both for conceptual understanding and in some circumstances for deciding which structure to use.
2
u/FerricDonkey Jun 14 '24
A handful of reasons, in no order other than how I thought of them.
First, the order is not part of the standard. This means they can change the internal workings at any time and without changing their guarantees to you. If there was a good reason to allow indexing, they might be willing to give this up, but I haven't heard any rumblings that they'll do so.
Second, what happens if two elements have the same hash? Does it maintain insertion order? Is it random ish? If you add and remove an element, does it go back where it was or a different place? Again, these could be addressed, but there has to be a reason first.
Third, why? Indexing into the set isn't really a normal use case. Iterating over the set is useful. Sometimes extracting just "an element of the set" is useful. But the case where you want a specific index is usually pretty rare. There may be another data structure that better suites your needs if you find yourself wanting to index into a set.
1
u/zefciu Jun 14 '24
The key part here is modulo the length of the list. This basically means that the set is ordered according to several last bits of the hashes. How many bits depends on its size. So adding elements to the set can cause the ordering to drastically change.
Also, what would be the value of adding index functionality? What exactly could you implement then? Because I can’t think of anything that isn’t terribly bug-prone and that can’t be done without it.
1
u/Brian Jun 14 '24
I found out that random generator are not truly random
That's how a standard PRNG works, but it's completely unrelated to sets. These aren't random per se - a better word would be arbitrarily ordered. Ie you shouldn't make any assumptions about order when using them.
but in ascending order of the hash value modulo the length of the list. is it true?
Not quite. Sets are implemented by hash tables. This is basically a list of buckets, and the item will initially be placed in the bucket numbered hash(item) % num_buckets. Note that the number of buckets is not the same as the number of items - it's generally multiple times the size in order to allow things to be efficient. Further, the bucket is not neccessarily consistent due to collissions. Consider two items with the same hash (modulo num_buckets). The set can't store both in the same bucket, so needs to resolve this. There are a few stragegies hash tables use, but a common one is linear probing, where the item is just stored in the next bucket down (with lookups being complicated a bit to check for this case as well as exact bucket maches when a collision is found). But this means the order of these items will vary depending on which one happened to be added first.
why not give them index functionality too?
Note that even under your assumption, there's no efficient way to index the nth item, because the items are stored sparsely (ie. with gaps between them). You basically have to iterate through all the buckets and count which ones are filled, making this an O(n) process rather than constant time indexing.
Further the order isn't stable. If you add or remove an item, the set might resize (ie. change num_buckets), potentially completely reordering all items. So "position" isn't really a reliable concept here.
Note that dicts do introduce a layer of indirection here where items are also stored as an array in insertion order, with the index into this array being what is stored in the hashtable (with some complications for deletion etc). But currently, sets don't use this approach.
1
u/coryalanfitz Jun 14 '24
No predictable computer system can produce true randomness
1
u/wildpantz Jun 14 '24
I stumbled upon a nice way to generate a random using Arduino though! (felt the need to say this because it's the discussion lately). If Arduino's analog pin is floating (not connected to anything in simple words) and the neighboring pins are floating too (something something crosstalk), you can simply read the voltage off the pin and usually it's gonna be somewhere between 200 and 700 in my experience.
The bad thing is, it's kind of influenced by electric and magnetic fields and it tends to sit aroud a certain value (for example 600 and then goes +- 100), but generating a 2 digit random number by taking last two digits should be pretty easy (but in most cases not worth it at all, and someone should definitely try and see what the distribution is!)
1
u/No-Log873 Jun 14 '24
Yep that's why they are called pseudo random number generators. If you want true random, you'll have to find it somewhere else. Maybe count the gamma rays emitted in a radioactive source, or set up race conditions in transistor circuits.
1
u/Kerbart Jun 14 '24
It comes down to semantics. The Mersenne Prime twister that is used is pretty good at creating chaotic results — in the mathematical sense; ther'e no way to calculate in advance what the fifth number of the sequence is going to be, aside from calculating the sequence up to the fifth number.
The results are also indistinguishable from true (uniformly distributed) random data. So for most intents and purposes, you can consider the data as "random" even when it isn't.
Use the same seed and you'll get the same results. Which is actually great for testing. It's bad for security when you rely on random numbers to be truly random. Usually in those cases some date/timestamp is used for a seed, but if you know down to the second when a key pair was created you can start making educated guesses on the randomizer seeds used.
That's why Python now has a secrets module. It doesn't make "better" or "more random" numbers than the regular randon number generator but it does make it far harder to reproduce its results, making it the better case when, for security, you don't want pseudorandom numbers that theoretically can be reproduced. If security is not an issue, random is just as good and faster.
42
u/danielroseman Jun 14 '24
If anyone told you sets are random, they are unequivocally wrong. Sets are unordered, which is not at all the same thing.
The description you quote shows exactly why they can't have index functionality; because the order can change unpredictably depending on other contents of the set. What would be the point of an index if it could change at any time?
In a list, indexes are stable unless you specifically insert items before that index. That would not be true of a set; any mutation operation could change the order.