Home   Archive   Permalink

Ensure only unique values get added to a block

Picked up REBOL a few nights ago, and made a little lottery number generating script.
Sometimes though, random will generate a duplicate number in appending values to [ ].
I saw that there was a unique function, but my attempt to use it to guarantee distinct values didn't work out.
Any tips for ensuring only numbers that aren't already in the block get appended?

posted by:   Gus     3-Aug-2013/17:13:47-7:00

Be sure to use random/seed to get actual random numbers. This loop will build a list of 10 unique numbers:
R E B O L []
random/seed now/time
x: copy []
while [10 > length? x] [
     y: random 100
     if not find x y [append x y]
print x

posted by:   Nick     4-Aug-2013/6:35:16-7:00

Is it possible to generate a seed off of a future time? As in, the upcoming date and time for the lotto drawing?
In the midst of playing with a solution, I noticed that find returns 'none' or the value.
I thought maybe a Boolean function would help in some circumstances so I made:
hasValue?: func [blk val] [
             either find blk val [True] [False]
Do you think that's overkill or wasted code?

posted by:   Gus     4-Aug-2013/17:31:29-7:00

FOUND? will do a similar thing in returning TRUE when the value is found and FALSE when the value is not found:
>> found? find blk val
Try writing:
>> source found?
and see the output. FOUND? is a bit more efficient, and contains a less on get-word!s.
You can also of course directly use:
>> not none? find blk val

posted by:   Henrik     5-Aug-2013/3:36:12-7:00

"and contains a less on get-word!s"
should be
"and contains a lesson on get-word!s"

posted by:   Henrik     5-Aug-2013/3:37:05-7:00

Thanks for the replies.
I wasn't aware of found?
I just dove in and tried to make what I wanted to make.
There are lots of details that I need to learn.

posted by:   Gus     5-Aug-2013/12:37:32-7:00

Here is another version:
     >> until [10 = length? unique append b: [] random 100] print b
     == 13 94 29 51 32 16 93 68 69 25
If you put this into a function don't forget to re-initialize the block
     >> lotto: has [b] [b: copy [] until [10 = length? unique append b random 100] b]

posted by:   Endo     5-Aug-2013/17:24:50-7:00

submit response

posted by:   y r a d n u o b - e k a m     4-Nov-2013/1:08:44-8:00