| <<<Back 1 day (to 2014/11/07) | 20141108 |
avih | tor, Math.random does this: js_pushnumber(J, (double)rand() / (RAND_MAX - 1));, but this can return 1 or even bigger than 1. you probably wanted .. / (RAND_MAX + 1) | 11:19.10 |
| or, if you can't know if int/long/whatever can hold RAND_MAX+1 without overflowing, then just retry if it results in 1 | 11:21.22 |
| and if you get 1 3 times then it must be a sign from god that something is wrong. | 11:28.43 |
nsz | yes that should be RAND_MAX+1 | 11:36.55 |
| i'd use RAND_MAX+1.0 and then no cast is needed :P | 11:37.25 |
| avih: RAND_MAX+1 is not representible as int usually | 11:41.13 |
| so it should be either RAND_MAX+1U | 11:41.36 |
| or +1.0 | 11:41.49 |
avih | nsz: yeah, 1.0 is nice enough :) | 13:23.37 |
| personally though, when i'm using rand in testing i need it seeded, but js doesn't provide seed api. so i roll my own seeded rand. | 13:25.08 |
| re 1I though, in some definitions it says that RAND_MAX is an integer, at which case 1U would work, but in others i saw that "it's an intergal", at which case it might be itself max uint | 13:27.48 |
| re 1U* | 13:27.57 |
nsz | where did you see it as an 'integral'? | 14:02.01 |
| the c standard is pretty clear | 14:02.09 |
| rand() returns int | 14:02.15 |
| and randmax is the maximal returned value | 14:02.30 |
| so RAND_MAX cannot be larger than INT_MAX | 14:02.41 |
| and usually it is INT_MAX (that's the most useful RAND_MAX) | 14:02.56 |
| the type of RAND_MAX may be different but that does not matter | 14:03.25 |
avih | http://www.cplusplus.com/reference/cstdlib/RAND_MAX/ | 16:17.43 |
| - cstdlib | 16:18.01 |
nsz | yes random websites are usually wrong about the c standard unfortunately | 17:06.59 |
| there is no such thing as 'integral constant expression' the corret terminology is 'integer constant expression' but it has not relevance here | 17:07.44 |
| the only relevant part is that it's the maximum value rand may return | 17:08.06 |
| which is at most INT_MAX | 17:08.11 |
| Forward 1 day (to 2014/11/09)>>> | |