World: r3wp
[Rebol School] Rebol School
older newer | first last |
Geomol 24-Feb-2009 [2362x2] | Anton, are you sure, I don't get a 4 byte float? >> d: make struct! [v [float]] none >> d/v >> type? d/v == decimal! >> length? third d == 4 The length of a decimal is 8. |
missed a line: >> d/v == 0.0 | |
Anton 24-Feb-2009 [2364x6] | ... no longer sure ...! :-) |
My error, sorry. | |
http://www.rebol.com/docs/library.html#section-4.2 | |
d: make struct! [v [decimal!]] none i: make struct! [v1 [integer!] v2 [integer!]] none i/v1: to integer! (random 2 ** 32) - (2 ** 31) - 1 i/v2: to integer! (random 2 ** 32) - (2 ** 31) - 1 change third d third i | |
The random decimal is d/v | |
Now divide by max-decimal (whatever that is) and multiply by desired range (eg. pi). | |
Geomol 24-Feb-2009 [2370x2] | :-) I follow you, when you say, you're no longer sure. This is a little used corner of REBOL (for me at least). I think, we have a problem still. random 2 ** 32 seem to never give results above 2 ** 31. |
Never returns: >> until [a: random 2 ** 32 a > (2 ** 31)] | |
Anton 24-Feb-2009 [2372] | That would be a problem, yes. |
Geomol 24-Feb-2009 [2373] | And since we don't have a 16 bit datatype (or maybe we do with a string or other series somehow!?), we have to make 8 times random 256 (and subtract 1). |
Anton 24-Feb-2009 [2374] | We can substitute the struct of 2 integer!s with a struct of 8 char. Maybe this is useful: >> random 255.255.255.255.255.255.255.255 == 168.97.60.251.15.20.205.31 |
Geomol 24-Feb-2009 [2375] | Kib asked a simple question about random and decimals. It seems to be a bit of a hazzle to get what we want, and this should be pointed out to Carl for R3, I think. |
Anton 24-Feb-2009 [2376] | but can't put tuple! in struct directly... |
kib2 24-Feb-2009 [2377] | Sometimes I wonder why such simple things are not implmented natively in REBOL. |
Geomol 24-Feb-2009 [2378] | Kib, I think, the answer is, that REBOL do a lot and then some more, but there are a few holes. |
kib2 24-Feb-2009 [2379] | Yes, this one and repeating a string are essential to me. I may find other cases too :) |
Anton 24-Feb-2009 [2380x3] | random also doesn't return 0. |
(ah.. that's why you subtracted 1, never mind.) | |
The other thing I was thinking we might need to watch out for is the production of invalid decimals. Maybe some mantissa combinations are not valid? | |
Geomol 24-Feb-2009 [2383x2] | :-) Kib, feel free to ask questions about all this. It may easily get blurred, when we talk like this. And it shows you, that we're often in doubt, because REBOL is so deep. (I hope, it's ok, we do this in the Rebol School group.) |
Right, Anton, I forgot about that. | |
Anton 24-Feb-2009 [2385] | Let's switch to the Math group. |
Geomol 24-Feb-2009 [2386x3] | Maybe the invalid decimal can be handled with some: if error? try [...] |
Kib, there might be a good explanation, why there's no random decimal in REBOL. See: http://stackoverflow.com/questions/439115/random-decimal-in-python It's not a trivial problem. | |
You asked how to make a random number between e.g. pi and -pi. There are a number of ULPs (Unit in the Last Place) between those two numbers. For 64 bit decimals, it's a large number. The possible decimals in computer arithmetic lie closer together around zero than for large numbers. If you had a routine, that would give you any possible 64 bit decimal number between pi and -pi with equal probability, then you would get a lot more numbers close to zero than close to either pi or -pi. The distribution wouldn't be flat (as you would expect). It's much better to choose, how many different values between pi and -pi, you need, and then make a random integer of that number, and do some calc to get the result between pi and -pi. I hope, it makes sense. | |
kib2 24-Feb-2009 [2389x2] | I came with this, what do you think of it ? (highly criticable) : random-dec: func[i n /local ent dec] [ "generates a decimal : i digits before, n digits after" ent: to-integer random i dec: to-integer random n return to-decimal rejoin [ent "." dec] ] print random-dec 9999 999999 ; 4 digits before 6 after |
Or even : random-dec: func[i n /fromdigits /local ent dec] [ "i digits before, n after" if fromdigits [ "i: number of digits before, n: number of digits after" i: 10 ** i - 1 n: 10 ** n - 1 ] ent: to-integer random i dec: to-integer random n return to-decimal rejoin [ent "." dec] ] print random-dec 999 99999 ; 3 digits before, 5 after print random-dec/fromdigits 4 6 ; 4 digits before, 6 after | |
[unknown: 5] 24-Feb-2009 [2391x2] | You shouldn't need the return statement. The last value will be returned unless a return is found elsewhere. |
Good work though Kib2. | |
Geomol 24-Feb-2009 [2393] | Yes, an interesting approach. Just remember, that random 9 will return 1-9, never zero. So maybe you wanna add one before doing the random, and then subtract one afterwards? Another thing, you might wanna have those explanation strings in the variable block? Try: >> random-dec: func ["i digits before, n after" i n /fromdigits "i: number of digits before, n: number of digits after"] [] >> ? random-dec |
Janko 24-Feb-2009 [2394x2] | is there some builtin way by which I can format number like this 11231 => 11.231 ? |
(I have done it) | |
Geomol 24-Feb-2009 [2396x2] | nice |
REBOL allow thousand separation with ', like: 11'231, but it's only for readability. You would probably convert to string and put #"." in for nice output. | |
Janko 24-Feb-2009 [2398] | GEomol, yes I need it for nice output ... this is the function I made: format-int: func [ int ] [ a: to-string int d: tail a loop to-integer ((length? a) - 1) / 3 [ d: back back back d insert d "," ] head d ] |
Geomol 24-Feb-2009 [2399] | Cool! |
Janko 24-Feb-2009 [2400] | well, it's not the nicest function probably but I am tired and just need it to work |
Geomol 24-Feb-2009 [2401] | Well, it does work, so you can be happy! :-) |
Gregg 24-Feb-2009 [2402] | There's nothing built in to do that Janko. |
Henrik 24-Feb-2009 [2403] | Gabriele once wrote a function for this. I can't remember if it's called pad-decimal or form-decimal. |
Gregg 24-Feb-2009 [2404x2] | Yes, there are a number of them out there. |
add-seps: func [ "Insert group separators" str [any-string!] /with sep [string! char!] /reb "Use REBOL group separator" ][ sep: any [sep all [reb #"'"] #","] str: skip any [find str "." tail str] -3 while [not head? str] [ insert str sep str: skip str -3 ] str ] | |
PatrickP61 25-Feb-2009 [2406] | Simple Question: I want to assign a specific script name to the word RUN and have it execute it whenever I type it into the console. RUN: do %script.r But upon the assignment, it evaluates the expression and executes it. So I tried it in a block as RUN: [do %script.r] but this returns == [do %script.r] I can do this: IT: %script.r and then type DO IT to perform the script, but I want a way to combine the DO and the IT together. Any ideas? |
[unknown: 5] 25-Feb-2009 [2407] | run: does [do %script.r] |
kib2 25-Feb-2009 [2408] | Isn't RUN already a native function ? |
PatrickP61 25-Feb-2009 [2409] | Thank you Paul. Perfect. |
Geomol 25-Feb-2009 [2410] | Yes, RUN is native, but you can redefine it, if you like. |
[unknown: 5] 25-Feb-2009 [2411] | Yes it is but I don't believe it is of any use. |
older newer | first last |