Help with code to create non-repeating set of 10 numbers
So I'm working on trying to create a non-repeating set to 10 randomly selected numbers between 0 and 9. I have this in a formula field:
let x := [number(void)];
while length(x) < 10 do
let newItem := [floor(random() * 10)];
if not contains(x, newItem) then
x := array(x, newItem)
end
end
;
debugValueInfo(x)
Thanks to in this post for helping me get started.
The issues are:
1) if you turn admin on/off to get the formula to trigger, you will notice that is always starts with 0. I don't know why that is. I have another piece of code that doesn't:
let numArray := for loop1 from 1 to 11 do
floor(random() * 10)
end;
debugValueInfo(numArray)
Even if I remove the if no contains, it always starts with 0. Is the array 'x' not truly empty?
2) it does not create a non-repeating set of 10 random numbers. in fact it doesn't seem to do anything except create 10 random numbers that seems to awfully be quite duplicative.
12 replies
-
Hi Fred
My reply to the previous post gives a non-repeating set of numbers between 0 and 9. It makes an array of 100 numbers but all of them are between 0 and 9. I then use the unique() function to just have a set of 10. It does assume that by the time 100 have been generated we have all 10 numbers, which is almost certain.
I am trying to create another solution that does a more succinct job.
Regards John
-
let x := slice([0], 0, 0);
while length(x) < 10 do
let newItem := floor(random() * 10);
if not contains(x, newItem) then
x := array(x, [newItem])
end
end
;
debugValueInfo(x) -
Or an other way
let r := range(0, 10); for i in range(10) do let result := item(r, random() * cnt(r)); r := r[!= result]; result end
-
So happy to see all of the different ways any solution can be done. You guys are the best.
Content aside
- Status Answered
- 6 mths agoLast active
- 12Replies
- 73Views
-
4
Following