Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- Be respectful, on topic and if you see a problem, Flag it.
If you would like to contact our Community Manager personally, feel free to send a private message or an email.
Pseudo-random number generator
ysuh
Member Posts: 3 EDU
I need a random number generator that does not need to be true random number. Any idea on implementing a (pseudo-)random number generator?
Tagged:
0
Comments
const chrMap = { 'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7, 'I': 8, 'J': 9, 'K': 10, 'L': 11, 'M': 12, 'N': 13, 'O': 14, 'P': 15, 'Q': 16, 'R': 17, 'S': 18, 'T': 19, 'U': 20, 'V': 21, 'W': 22, 'X': 23, 'Y': 24, 'Z': 25, 'a': 26, 'b': 27, 'c': 28, 'd': 29, 'e': 30, 'f': 31, 'g': 32, 'h': 33, 'i': 34, 'j': 35, 'k': 36, 'l': 37, 'm': 38, 'n': 39, 'o': 40, 'p': 41, 'q': 42, 'r': 43, 's': 44, 't': 45, 'u': 46, 'v': 47, 'w': 48, 'x': 49, 'y': 50, 'z': 51, '_': 99, '-': 98 }; function idToNum(input is string) returns number { var out is string = ""; for (var char in splitIntoCharacters(input)) {<br> var res = match(char, REGEX_NUMBER); if (res.hasMatch) { out = out ~ toString(res.captures[0]); } else { out = out ~ toString(chrMap[char]); } } return stringToNumber(out) % 100000; } // Usage with the rng: var rng = lcprng(idToNum(id[0]));I imagine you could do something like this (source):
const chrMap = { 'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7, 'I': 8, 'J': 9, 'K': 10, 'L': 11, 'M': 12, 'N': 13, 'O': 14, 'P': 15, 'Q': 16, 'R': 17, 'S': 18, 'T': 19, 'U': 20, 'V': 21, 'W': 22, 'X': 23, 'Y': 24, 'Z': 25, 'a': 26, 'b': 27, 'c': 28, 'd': 29, 'e': 30, 'f': 31, 'g': 32, 'h': 33, 'i': 34, 'j': 35, 'k': 36, 'l': 37, 'm': 38, 'n': 39, 'o': 40, 'p': 41, 'q': 42, 'r': 43, 's': 44, 't': 45, 'u': 46, 'v': 47, 'w': 48, 'x': 49, 'y': 50, 'z': 51, '0': 52, '1': 53, '2': 54, '3': 55, '4': 56, '5': 57, '6': 58, '7': 59, '8': 60, '9': 61, '_': 62, '-': 63 }; // hashes a string into the [0, 2^32-1] range (picked arbitrarily) function hash(input is string) returns number { const mod = 2^32; var result = 5381; for (var char in splitIntoCharacters(input)) { result = (result * 33 + chrMap[char]) % mod; } return result; }How would I approach limiting the domain of the values to 0 to 1 (or any arbitrary value range)?
The Onsherpa | Reach peak Onshape productivity
www.theonsherpa.com
The Onsherpa | Reach peak Onshape productivity
www.theonsherpa.com
The Onsherpa | Reach peak Onshape productivity
www.theonsherpa.com
The Onsherpa | Reach peak Onshape productivity
www.theonsherpa.com