flat assembler
Message board for the users of flat assembler.
Index
> Main > problem with sign Goto page 1, 2 Next |
Author |
|
HaHaAnonymous 27 Oct 2013, 20:06
[ Post removed by author. ]
Last edited by HaHaAnonymous on 28 Feb 2015, 19:16; edited 1 time in total |
|||
27 Oct 2013, 20:06 |
|
AsmGuru62 27 Oct 2013, 22:22
You can generate a value in range [0..100] and then subtract 50.
|
|||
27 Oct 2013, 22:22 |
|
hopcode 28 Oct 2013, 12:04
randomdude wrote: i have extracted these functions from a program: anyway it's not safe choosing bits that way (SHR/AND) without a theory. it may corrupt entirely the pseudo-randmoness genuinity of the algo,if any. this may help http://board.x64lab.net/viewtopic.php?f=2&t=35. then open Park'pdf there and follow instructions for the minimal standard to get back -+50 as ret value. Cheers, _________________ ⠓⠕⠏⠉⠕⠙⠑ |
|||
28 Oct 2013, 12:04 |
|
randomdude 28 Oct 2013, 18:24
AsmGuru62 wrote: You can generate a value in range [0..100] and then subtract 50. yeah, but thats not very practical hopcode wrote: it seems a sloppy version of the Park-Miller generator. very interesting stuff indeed, hopcode! however i dont see how can that help me tho my_fastrand32 is actually the rand() function from msvcrt.dll, i just extended the two last instruction to increase the 32767 limit Code: proc rand mov eax,dword[seed] imul eax,343FDh add eax,269EC3h mov dword[seed],eax shr eax,10h and eax,7FFFh ret endp by removing shr/and it will return negative values too from range -2147483648 to 2147483647, but for some reason if i do that my_getfastrand32 starts acting weird and returns negative values below the 'min' argument this is my_getfastrand32 in c++: Code: int my_getfastrand32(int min, int max) { if (min == max) return min; else return (int)(my_fastrand32() / (double)(2147483648.0 + 1.0) * (max - min + 1) + min); }
|
|||||||||||
28 Oct 2013, 18:24 |
|
cod3b453 28 Oct 2013, 18:38
The my_fastrand32/2^31 term will be negative for negative my_fastrand32 and for suitably large magnitude round to -1 hence the product with the range is negative and the addition of min gives a value below min.
|
|||
28 Oct 2013, 18:38 |
|
revolution 28 Oct 2013, 19:42
randomdude wrote: by removing shr/and it will return negative values too from range -2147483648 to 2147483647 ... It might be better to use some LCG parameters that are designed for 32-bit, sequences rather than using your extended version that originally was designed for 16-bit sequences. Have you tested the period of it yet? I suspect it will be very short. That is all assuming that an LCG is okay for your situation. For something like Monte-Carlo simulations LCGs are terrible generators to use. |
|||
28 Oct 2013, 19:42 |
|
AsmGuru62 28 Oct 2013, 21:07
I posted Mersenne Twister here some time ago:
http://board.flatassembler.net/topic.php?t=13741 Also, there is a good discussion about RNG on that thread. |
|||
28 Oct 2013, 21:07 |
|
Sasha 29 Oct 2013, 00:02
Quote:
This generator is the poorest one) And the only thing you can NOT do with it is Quote:
In wikipedia there is a good explanation about the limits of LCG's. Generally, what do you want from the generator? If you want a good random numbers, then work on Mersenne Twister or xorshift. If LCG is enough there are very simple LCG's with the maximum 32 bit period. Discussed here. I made many of them. It is very hard to find a good constants that will give you a good sequence. So you must chose from the tested ones or make all the required tests yourself. Some bits can be less random than others, so they are cut for this reason. Then you need to cut the random number to the desired range. Code: if max>min return min+((number*(max-min))/2^32) or this, but I haven't used it. Correct me if I'm wrong. Code: if max>min return min+(number mod (max-min)) Btw, can you explain what does this mean? Quote:
And why do you think, that subtracting is not practical if you are using one? And why do you need a fpu ? |
|||
29 Oct 2013, 00:02 |
|
HaHaAnonymous 29 Oct 2013, 00:14
[ Post removed by author. ]
Last edited by HaHaAnonymous on 28 Feb 2015, 19:15; edited 1 time in total |
|||
29 Oct 2013, 00:14 |
|
hopcode 29 Oct 2013, 01:43
Hallo randomdude,
i cannot resist, you set me in such a good and agreeable mood... i ehmmm have urgence to quote it again randomdude wrote: ...from msvcrt.dll, i just extended the two last instruction to increase the 32767 limit... ramdudu wrote:
why not ? never understimate the solutions from AsmGuru62 . try this untested implementation. it should ret in EAX the desired value Code: proc .rand sub esp,4 mov eax,dword[seed] imul eax,343FDh add eax,269EC3h mov dword[seed],eax shr eax,10h and eax,7FFFh mov [esp],eax fild dword[esp] fdiv dword[_const] fmul dword[_consta] fistp dword[esp] pop eax sub eax,50 ret align 4 _const dd 32767.0 _consta dd 100.0 endp but seriously speaking now, from what i know the rand-algo there is crappy. AFAIK, from a 1 to 10 skala it may stay level 1, where sushi-marsenne provided from AsmGuru level 6. i think it is of relevance to have a general vision of the whole...firstly. Cheers, _________________ ⠓⠕⠏⠉⠕⠙⠑ |
|||
29 Oct 2013, 01:43 |
|
Sasha 29 Oct 2013, 01:54
I thought, that shifting 32 times will zero the 32 bit register. Tested it...it's almost yes, but not) Shifting 31 times almost clears, but 32 restores all bits, then shr eax,33 is again like shr eax,1.
|
|||
29 Oct 2013, 01:54 |
|
Sasha 29 Oct 2013, 02:10
hopcode, why fpu?
Why not this? This is like my first example. Code: proc my_getfastrand32,min,max locals tmpvalue dd ? endl mov eax,dword[min] cmp eax,dword[max] je .end stdcall my_fastrand32 mov edx,[max] sub edx,[min] mul edx mov eax,edx add eax,[min] .end: ret endp |
|||
29 Oct 2013, 02:10 |
|
hopcode 29 Oct 2013, 02:37
Sasha wrote: hopcode, why fpu? i have read just now the discussion there you linked above, and ehmm... i dont want to disappoint you but the algo-level is practically the same as here,imho. that discussion is anyway important because you can find there some hints on how to get acquainted with the general vision about "randomness". it is not simple... _________________ ⠓⠕⠏⠉⠕⠙⠑ |
|||
29 Oct 2013, 02:37 |
|
Sasha 29 Oct 2013, 02:57
By algo-level you mean the quality of random numbers or the optimisation of a code for speed? This is what I wanted, and randomdude called his proc fastrand. So after some benchmarks (not today, in the times of that thread) I've figure out, that the RNG that have only one mul is slower than xorshift, that have several xor's,shr's and mov's! At least on my comp and in my implementation.
|
|||
29 Oct 2013, 02:57 |
|
hopcode 29 Oct 2013, 03:18
yes. quality of numbers. i quote revolution from there
Quote: Speed of generation is not the issue... LCGs are low quality and this must be considered when deciding which class of generator to use for the needed purpose. one may think to improve some LCG acording a theory, but only when the purpose is worth. applying firstly a bit of mathe, before benchmarking can save lot of time, avoiding needing to test modifications. _________________ ⠓⠕⠏⠉⠕⠙⠑ |
|||
29 Oct 2013, 03:18 |
|
Sasha 29 Oct 2013, 11:55
So what I'm trying to say, that the LCG's worth is in its simplicity, but they give bad numbers and bad speed!
|
|||
29 Oct 2013, 11:55 |
|
AsmGuru62 29 Oct 2013, 17:51
LCG speed should be very good actually, but numbers -- yes, they can be not suitable for some serious work, like simulations.
|
|||
29 Oct 2013, 17:51 |
|
hopcode 30 Oct 2013, 12:48
Hallo people,
just yesterday i was reading something about pi and i got suddenly an idea how to implement an hypercube (something alike from the sushi-marsene) from a stream of it. here some results of my not-yet-tuned-theory, i have been implementing just 2 hours ago. test on 16k data Code: flat assembler version 1.71.15 (1048576 kilobytes memory) 3 passes, 2048 bytes. seconds...0.029206991195679 Value Char Occurrences Fraction 0 80 0.004883 1 59 0.003601 2 61 0.003723 3 72 0.004395 4 59 0.003601 5 58 0.003540 6 75 0.004578 7 65 0.003967 8 68 0.004150 9 61 0.003723 10 64 0.003906 11 66 0.004028 12 61 0.003723 13 64 0.003906 14 71 0.004333 15 56 0.003418 16 75 0.004578 17 64 0.003906 18 67 0.004089 19 70 0.004272 20 68 0.004150 21 62 0.003784 22 69 0.004211 23 66 0.004028 24 65 0.003967 25 58 0.003540 26 80 0.004883 27 72 0.004395 28 70 0.004272 29 52 0.003174 30 66 0.004028 31 78 0.004761 32 59 0.003601 33 ! 65 0.003967 34 " 61 0.003723 35 # 63 0.003845 36 $ 62 0.003784 37 % 76 0.004639 38 & 50 0.003052 39 ' 59 0.003601 40 ( 68 0.004150 41 ) 75 0.004578 42 * 82 0.005005 43 + 58 0.003540 44 , 54 0.003296 45 - 49 0.002991 46 . 71 0.004333 47 / 70 0.004272 48 0 63 0.003845 49 1 67 0.004089 50 2 70 0.004272 51 3 62 0.003784 52 4 67 0.004089 53 5 66 0.004028 54 6 58 0.003540 55 7 72 0.004395 56 8 72 0.004395 57 9 56 0.003418 58 : 72 0.004395 59 ; 65 0.003967 60 < 63 0.003845 61 = 62 0.003784 62 > 57 0.003479 63 ? 63 0.003845 64 @ 55 0.003357 65 A 74 0.004517 66 B 63 0.003845 67 C 78 0.004761 68 D 67 0.004089 69 E 71 0.004333 70 F 55 0.003357 71 G 51 0.003113 72 H 65 0.003967 73 I 68 0.004150 74 J 85 0.005188 75 K 60 0.003662 76 L 74 0.004517 77 M 68 0.004150 78 N 59 0.003601 79 O 59 0.003601 80 P 64 0.003906 81 Q 82 0.005005 82 R 83 0.005066 83 S 48 0.002930 84 T 60 0.003662 85 U 64 0.003906 86 V 65 0.003967 87 W 56 0.003418 88 X 66 0.004028 89 Y 59 0.003601 90 Z 51 0.003113 91 [ 73 0.004456 92 \ 65 0.003967 93 ] 58 0.003540 94 ^ 61 0.003723 95 _ 57 0.003479 96 ` 70 0.004272 97 a 57 0.003479 98 b 58 0.003540 99 c 63 0.003845 100 d 88 0.005371 101 e 73 0.004456 102 f 60 0.003662 103 g 70 0.004272 104 h 53 0.003235 105 i 63 0.003845 106 j 60 0.003662 107 k 61 0.003723 108 l 52 0.003174 109 m 77 0.004700 110 n 56 0.003418 111 o 66 0.004028 112 p 67 0.004089 113 q 60 0.003662 114 r 53 0.003235 115 s 64 0.003906 116 t 57 0.003479 117 u 67 0.004089 118 v 58 0.003540 119 w 62 0.003784 120 x 59 0.003601 121 y 64 0.003906 122 z 65 0.003967 123 { 67 0.004089 124 | 72 0.004395 125 } 70 0.004272 126 ~ 50 0.003052 127 53 0.003235 128 45 0.002747 129 73 0.004456 130 68 0.004150 131 60 0.003662 132 83 0.005066 133 58 0.003540 134 61 0.003723 135 70 0.004272 136 63 0.003845 137 66 0.004028 138 59 0.003601 139 69 0.004211 140 63 0.003845 141 66 0.004028 142 71 0.004333 143 55 0.003357 144 56 0.003418 145 65 0.003967 146 63 0.003845 147 50 0.003052 148 71 0.004333 149 76 0.004639 150 60 0.003662 151 74 0.004517 152 62 0.003784 153 63 0.003845 154 58 0.003540 155 59 0.003601 156 68 0.004150 157 71 0.004333 158 72 0.004395 159 77 0.004700 160 55 0.003357 161 ¡ 69 0.004211 162 ¢ 76 0.004639 163 £ 58 0.003540 164 ¤ 67 0.004089 165 ¥ 67 0.004089 166 ¦ 74 0.004517 167 § 62 0.003784 168 ¨ 53 0.003235 169 © 67 0.004089 170 ª 61 0.003723 171 « 59 0.003601 172 ¬ 59 0.003601 173 71 0.004333 174 ® 66 0.004028 175 ¯ 55 0.003357 176 ° 73 0.004456 177 ± 59 0.003601 178 ² 63 0.003845 179 ³ 62 0.003784 180 ´ 63 0.003845 181 µ 55 0.003357 182 ¶ 53 0.003235 183 · 71 0.004333 184 ¸ 67 0.004089 185 ¹ 58 0.003540 186 º 52 0.003174 187 » 63 0.003845 188 ¼ 65 0.003967 189 ½ 79 0.004822 190 ¾ 53 0.003235 191 ¿ 60 0.003662 192 À 61 0.003723 193 Á 61 0.003723 194 Â 60 0.003662 195 Ã 68 0.004150 196 Ä 55 0.003357 197 Å 54 0.003296 198 Æ 73 0.004456 199 Ç 70 0.004272 200 È 59 0.003601 201 É 61 0.003723 202 Ê 68 0.004150 203 Ë 56 0.003418 204 Ì 67 0.004089 205 Í 56 0.003418 206 Î 78 0.004761 207 Ï 64 0.003906 208 Ð 72 0.004395 209 Ñ 67 0.004089 210 Ò 75 0.004578 211 Ó 52 0.003174 212 Ô 66 0.004028 213 Õ 60 0.003662 214 Ö 75 0.004578 215 × 68 0.004150 216 Ø 61 0.003723 217 Ù 67 0.004089 218 Ú 62 0.003784 219 Û 54 0.003296 220 Ü 68 0.004150 221 Ý 53 0.003235 222 Þ 85 0.005188 223 ß 67 0.004089 224 à 55 0.003357 225 á 78 0.004761 226 â 55 0.003357 227 ã 60 0.003662 228 ä 70 0.004272 229 å 42 0.002563 230 æ 62 0.003784 231 ç 62 0.003784 232 è 58 0.003540 233 é 54 0.003296 234 ê 56 0.003418 235 ë 68 0.004150 236 ì 75 0.004578 237 í 67 0.004089 238 î 54 0.003296 239 ï 62 0.003784 240 ð 71 0.004333 241 ñ 62 0.003784 242 ò 63 0.003845 243 ó 50 0.003052 244 ô 71 0.004333 245 õ 61 0.003723 246 ö 65 0.003967 247 ÷ 53 0.003235 248 ø 76 0.004639 249 ù 60 0.003662 250 ú 61 0.003723 251 û 70 0.004272 252 ü 56 0.003418 253 ý 66 0.004028 254 þ 57 0.003479 255 ÿ 53 0.003235 Total: 16384 1.000000 Entropy = 7.989049 bits per byte. Optimum compression would reduce the size of this 16384 byte file by 0 percent. Chi square distribution for 16384 samples is 249.81, and randomly would exceed this value 57.99 percent of the times. Arithmetic mean value of data bytes is 126.4144 (127.5 = random). Monte Carlo value for Pi is 3.166300366 (error 0.79 percent). Serial correlation coefficient is -0.005951 (totally uncorrelated = 0.0). and as a confirmation theory is not that bad, look at the improved values matching the general rules of what we know being called as "randomness". on 32k * 32 data Code: flat assembler version 1.71.15 (1048576 kilobytes memory) 3 passes, 0.1 seconds, 2048 bytes. seconds...7.4178831577301 Value Char Occurrences Fraction 0 16296 0.003885 1 16425 0.003916 2 16456 0.003923 3 16326 0.003892 4 16257 0.003876 5 16582 0.003953 6 16338 0.003895 7 16304 0.003887 8 16578 0.003953 9 16319 0.003891 10 16396 0.003909 11 16483 0.003930 12 16733 0.003989 13 16397 0.003909 14 16376 0.003904 15 16443 0.003920 16 16482 0.003930 17 16465 0.003926 18 16515 0.003937 19 16448 0.003922 20 16234 0.003870 21 16468 0.003926 22 16555 0.003947 23 16199 0.003862 24 16239 0.003872 25 16565 0.003949 26 16410 0.003912 27 16309 0.003888 28 16542 0.003944 29 16470 0.003927 30 16345 0.003897 31 16174 0.003856 32 16464 0.003925 33 ! 16284 0.003882 34 " 16147 0.003850 35 # 16474 0.003928 36 $ 16468 0.003926 37 % 16329 0.003893 38 & 16428 0.003917 39 ' 16310 0.003889 40 ( 16441 0.003920 41 ) 16303 0.003887 42 * 16506 0.003935 43 + 16435 0.003918 44 , 16099 0.003838 45 - 16382 0.003906 46 . 16390 0.003908 47 / 16315 0.003890 48 0 16287 0.003883 49 1 16535 0.003942 50 2 16535 0.003942 51 3 16473 0.003927 52 4 16294 0.003885 53 5 16365 0.003902 54 6 16198 0.003862 55 7 16244 0.003873 56 8 16422 0.003915 57 9 16351 0.003898 58 : 16341 0.003896 59 ; 16437 0.003919 60 < 16601 0.003958 61 = 16214 0.003866 62 > 16274 0.003880 63 ? 16354 0.003899 64 @ 16279 0.003881 65 A 16135 0.003847 66 B 16548 0.003945 67 C 16482 0.003930 68 D 16206 0.003864 69 E 16441 0.003920 70 F 16423 0.003916 71 G 16168 0.003855 72 H 16248 0.003874 73 I 16488 0.003931 74 J 16213 0.003865 75 K 16353 0.003899 76 L 16516 0.003938 77 M 16379 0.003905 78 N 16227 0.003869 79 O 16229 0.003869 80 P 16610 0.003960 81 Q 16307 0.003888 82 R 16527 0.003940 83 S 16359 0.003900 84 T 16448 0.003922 85 U 16331 0.003894 86 V 16261 0.003877 87 W 16439 0.003919 88 X 16394 0.003909 89 Y 16297 0.003886 90 Z 16389 0.003907 91 [ 16459 0.003924 92 \ 16288 0.003883 93 ] 16709 0.003984 94 ^ 16236 0.003871 95 _ 16515 0.003937 96 ` 16148 0.003850 97 a 16410 0.003912 98 b 16295 0.003885 99 c 16120 0.003843 100 d 16510 0.003936 101 e 16536 0.003942 102 f 16466 0.003926 103 g 16384 0.003906 104 h 16375 0.003904 105 i 16140 0.003848 106 j 16401 0.003910 107 k 16441 0.003920 108 l 16301 0.003886 109 m 16275 0.003880 110 n 16490 0.003932 111 o 16590 0.003955 112 p 16222 0.003868 113 q 16475 0.003928 114 r 16412 0.003913 115 s 16384 0.003906 116 t 16285 0.003883 117 u 16496 0.003933 118 v 16492 0.003932 119 w 16286 0.003883 120 x 16343 0.003896 121 y 16589 0.003955 122 z 16293 0.003885 123 { 16416 0.003914 124 | 16385 0.003906 125 } 16310 0.003889 126 ~ 16385 0.003906 127 16388 0.003907 128 16456 0.003923 129 16377 0.003905 130 16463 0.003925 131 16578 0.003953 132 16428 0.003917 133 16401 0.003910 134 16384 0.003906 135 16388 0.003907 136 16389 0.003907 137 16393 0.003908 138 16411 0.003913 139 16334 0.003894 140 16505 0.003935 141 16214 0.003866 142 16690 0.003979 143 16247 0.003874 144 16412 0.003913 145 16544 0.003944 146 16476 0.003928 147 16251 0.003875 148 16416 0.003914 149 16489 0.003931 150 16294 0.003885 151 16435 0.003918 152 16502 0.003934 153 16234 0.003870 154 16154 0.003851 155 16293 0.003885 156 16232 0.003870 157 16341 0.003896 158 16517 0.003938 159 16559 0.003948 160 16220 0.003867 161 ¡ 16324 0.003892 162 ¢ 16619 0.003962 163 £ 16093 0.003837 164 ¤ 16268 0.003879 165 ¥ 16691 0.003979 166 ¦ 16505 0.003935 167 § 16161 0.003853 168 ¨ 16421 0.003915 169 © 16606 0.003959 170 ª 16140 0.003848 171 « 16405 0.003911 172 ¬ 16388 0.003907 173 16381 0.003906 174 ® 16305 0.003887 175 ¯ 16395 0.003909 176 ° 16583 0.003954 177 ± 16402 0.003911 178 ² 16267 0.003878 179 ³ 16440 0.003920 180 ´ 16346 0.003897 181 µ 16326 0.003892 182 ¶ 16501 0.003934 183 · 16513 0.003937 184 ¸ 16242 0.003872 185 ¹ 16592 0.003956 186 º 16292 0.003884 187 » 16107 0.003840 188 ¼ 16405 0.003911 189 ½ 16359 0.003900 190 ¾ 16407 0.003912 191 ¿ 16507 0.003936 192 À 16415 0.003914 193 Á 16463 0.003925 194 Â 16457 0.003924 195 Ã 16264 0.003878 196 Ä 16366 0.003902 197 Å 16262 0.003877 198 Æ 16498 0.003933 199 Ç 16625 0.003964 200 È 16569 0.003950 201 É 16239 0.003872 202 Ê 16437 0.003919 203 Ë 16372 0.003903 204 Ì 16324 0.003892 205 Í 16117 0.003843 206 Î 16583 0.003954 207 Ï 16439 0.003919 208 Ð 16276 0.003881 209 Ñ 16468 0.003926 210 Ò 16563 0.003949 211 Ó 16125 0.003844 212 Ô 16461 0.003925 213 Õ 16241 0.003872 214 Ö 16314 0.003890 215 × 16465 0.003926 216 Ø 16564 0.003949 217 Ù 16487 0.003931 218 Ú 16313 0.003889 219 Û 16448 0.003922 220 Ü 16564 0.003949 221 Ý 16212 0.003865 222 Þ 16439 0.003919 223 ß 16331 0.003894 224 à 16315 0.003890 225 á 16527 0.003940 226 â 16360 0.003901 227 ã 16419 0.003915 228 ä 16259 0.003876 229 å 16201 0.003863 230 æ 16401 0.003910 231 ç 16360 0.003901 232 è 16484 0.003930 233 é 16506 0.003935 234 ê 16486 0.003931 235 ë 16275 0.003880 236 ì 16412 0.003913 237 í 16454 0.003923 238 î 16205 0.003864 239 ï 16482 0.003930 240 ð 16409 0.003912 241 ñ 16199 0.003862 242 ò 16319 0.003891 243 ó 16520 0.003939 244 ô 16506 0.003935 245 õ 16266 0.003878 246 ö 16247 0.003874 247 ÷ 16373 0.003904 248 ø 16392 0.003908 249 ù 16385 0.003906 250 ú 16382 0.003906 251 û 16197 0.003862 252 ü 16383 0.003906 253 ý 16180 0.003858 254 þ 16490 0.003932 255 ÿ 16415 0.003914 Total: 4194304 1.000000 Entropy = 7.999957 bits per byte. Optimum compression would reduce the size of this 4194304 byte file by 0 percent. Chi square distribution for 4194304 samples is 250.61, and randomly would exceed this value 56.59 percent of the times. Arithmetic mean value of data bytes is 127.4910 (127.5 = random). Monte Carlo value for Pi is 3.143437522 (error 0.06 percent). Serial correlation coefficient is -0.000431 (totally uncorrelated = 0.0). idea seems working. i think i will give some time for it. it deserves deeper study, and some more deeper tests. Cheers, _________________ ⠓⠕⠏⠉⠕⠙⠑ |
|||
30 Oct 2013, 12:48 |
|
revolution 30 Oct 2013, 13:26
Indeed PI was proved irrational a long time ago so would be fine for generating PRNG sequences. But it suffers from the problem of generation difficulty. If you want some significant amount of numbers then you need to compute PI to very large precision.
|
|||
30 Oct 2013, 13:26 |
|
Goto page 1, 2 Next < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.