Hey yall,
I've been working with textblaze for a few months now. But, I have run into a couple problems that have run me up a wall.
-
I have a snippet that generates a randomized list of 20 numbers from a range (form text input for the range). But, on occasion it outputs some obviously not random sequence of numbers. Example (range 1-50): 2, 5,6,7,8,13,17,18,19,20,25,28,29,30 etc. There seems to be a pattern to the numbers becoming consecutive. Code at bottom of this post.
-
I tried making a new snippet that generates a sequence seq(first,last) and then selects random numbers from that sequence with the randomitem(list, weights) function. Where I've had problems here is changing the value of items in a list. I was using a paired weights list for the second param of randomitem().
Is there a better way to add code to these posts? I've only used the forum a couple of times?
Thanks for your input!
Patrick
Snippet 1:
{note: preview=no}{run: Pool={{^^%5B%5D^^}}
}{endnote}{note}Input First and Last Numbers:
{endnote}{note: insert=no}
{formtext: name=First Number}{formtext: name=Last Number}{endnote}{if: `last number`> `first number` and `first number`> 0}{Pool=unique(map(seq(1,30), x->round(random()*(`last number`-`first number`)+`first number`)))}{else}{Pool={{^^%5B%5D^^}}}{endif}{note: preview=yes}
{formmenu: Comma Separated; default=Line Break; name=Options}
{endnote}{note: insert=yes; preview=yes}{if: options="Comma Separated"}{=join(sort(slice(pool,1,20), (a,b)-> a-b),",")}{else}{=join(sort(slice(pool,1,20), (a,b)-> a-b),"\n")}{endif}{endnote}
Snippet 2:
{run: x=0
Pool={{^^%5B%20%5D^^}}
Weights={{^^%5B%20%5D^^}}
String=""
}{formtext: name=First Number; validate=[["regex": "^(-?\\d+(?:\\.\\d+)?|)$", "message": "This field must be a number."]]}{formtext: name=Last Number; validate=[["regex": "^(-?\\d+(?:\\.\\d+)?|)$", "message": "This field must be a number."]]}{button: Pool=seq(`first number`,`last number`)
Weights=map(seq(1,count(Pool)),v->1)
x=x+1
; label=Generate}
{if: isodd(x)="yes"}{repeat: 2}
{run: selection=randomitem(Pool,Weights)
}{run: Weights[location(Pool,selection)]=0}{=location(Pool,selection)}, {=selection}, {=weights}
{endrepeat}{endif}{=Weights}

