World: r3wp
[Rebol School] Rebol School
older newer | first last |
Geomol 24-Feb-2009 [2387x2] | 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. |
PatrickP61 25-Feb-2009 [2412] | It is not in r3, alpha 35 |
[unknown: 5] 25-Feb-2009 [2413] | I think it was supposed to be a winexecute type of function. |
kib2 25-Feb-2009 [2414] | ok, it's clearer now. |
Henrik 25-Feb-2009 [2415] | I think RUN is extensively used in IOS, but I can't be certain. |
Geomol 25-Feb-2009 [2416x4] | Try source does and you see, DOES simply makes a function. So instead of using DOES, you could also have written: run: func [] [do %script.r] , but of course DOES is easier and shorter in this case. |
And now you're at it, try source func and see how similar to DOES it is. | |
And the last one in this group of function-defining functions, try ? has source has | |
I use FUNC, DOES and HAS a lot in my scripts. | |
PatrickP61 25-Feb-2009 [2420] | Good to know -- Thank you |
Geomol 25-Feb-2009 [2421x2] | welcome |
A little tutorial on HAS, functions and objects. Let's say, I wanted to make a function with a local variable, that works like a counter, so 1 is added to it every time, I call my function. This doesn't work: >> f: has [a] [a: 0 a: a + 1] >> f == 1 >> f == 1 because a has to be defined (given a value and by that a datatype), before I can use it, and I need to use it, when I add 1. So I can use an object: >> o: make object! [a: 0 f: does [a: a + 1]] >> o/f == 1 >> o/f == 2 But there is a trick to do the same thing with HAS and without making an object: >> f: has [a] [a: [0] a/1: a/1 + 1] >> f == 1 >> f == 2 The thing is, that a is giving a block with zero inside the first time. The second time, I call f, a is just the same block (part of memory), and the value inside isn't touched (it is 1 now), so it works. I can see the block inside my function with: >> source f f: func [/local a][a: [2] a/1: a/1 + 1] and we see, a is a block with 2 inside, because I've called my function 2 times. | |
PatrickP61 25-Feb-2009 [2423x2] | If you used a: [0], doesn't this assign a zero to the block for a? |
I gotta play around with that | |
Geomol 25-Feb-2009 [2425x4] | a: [0] simply mean, a become this block with zero inside, if a isn't already this block. If a already is this same block, this same part of memory, it has no effect. To always make a block with zero inside, you need to use COPY or REDUCE. |
I was often confused by this, I remember. | |
When you get used to it, it's pretty smart. | |
This is only within functions. If you write a: [0] at the console prompt, a become a new block with zero inside (you typed a new block, so it isn't the same, and it kinda makes sense). :-) | |
kib2 25-Feb-2009 [2429] | >> print abc ** Script Error: abc has no value Ok, no problem I understand >> print <abc> <abc> Why does this work ? |
Geomol 25-Feb-2009 [2430x2] | abc is seen as a word, and it doens't have a value. <abc> is a tag! datatype, like "abc" is a string datatype: >> type? <abc> == tag! |
and it also is a series, like strings are series: >> second <abc> == #"b" | |
kib2 25-Feb-2009 [2432] | Geomol: I wasn't aware there was a tag type ! |
Henrik 25-Feb-2009 [2433x2] | kib2: build-tag [img src dot.gif] |
kib2, to see all types: ? datatype! | |
kib2 25-Feb-2009 [2435] | Really useful : thanks ! |
Geomol 25-Feb-2009 [2436] | I've forgot, what a symbol! datatype is!? |
older newer | first last |