How to generate the Random floating number?

Hello All,

I am using below code for random floating number generation. Is there any other effective way?

put random(10,50) into percent1
put random(10,50) into percent2
put random(1,20) +round((percent1%+percent1%),2) into random_float_value
log random_float_value

How about this?

put random(100,2099) / 100 into random_float_value
log random_float_value

Hello Thanagaraj
would this help
put random(0,2000)%
or
put random(0,20000) / 1000
or …
depending on the number of fractional digits You are interested in.

To force a fixed number of fractional digits pls. use the format() function, e.g.
put format("%2.3f",random(0,20000)/1000)

BR

Hello All, Thanks for the value input.

Do you have any proposal how to generate the random list of from the available data list?

Example I tried the below code ,but am not getting expected result:

put 1 to 1000 into available_list
put 1 into count_index
repeat 5 times
put [] into random_number_list
insert any items of available_list into item count_index of random_number_list
log random_number_list
add 1 to count_index
end repeat
log random_number_list

Expected output:

[123,456,234,678,267] - some random list

I have found the below solution:
Not sure whether it’s optimal

put [“Test 10”,“Test 100”,“Test 102”,“Test 104”,“Test 105”,“Test 106”,“Test 107”,“Test 112”,“Test 116”,“Test 117”,“Test 118”,“Test 119”,“Test 121”,“Test 122”,“Test 124”,“Test 125”,“Test 13”,“Test 130”,“Test 139”] into avaliable_Test_list

repeat 3 times
set Test_name_list to “”
insert any items of avaliable_Test_list into Test_name_list
insert any items of avaliable_Test_list into Test_name_list
insert any items of avaliable_Test_list into Test_name_list
end repeat
log Test_name_list

You are resetting random_number_list inside the repeat loop so it becomes empty in each iteration. I would do it before the repeat loop.
Also, “any items of list” could return the duplicate values. Wit 1000 items, the chances are low but it can happen. If you want to ensure no duplicates, you can use “ExcludingItems” function (Text and Data Manipulation) like this:

put 1 to 1000 into available_list
put [] into unique_random_number_list
repeat 5 times
    insert any item of (items of available_list excluding those in unique_random_number_list) into unique_random_number_list
end repeat
log unique_random_number_list
2 Likes

That will work. A slightly simpler and more efficient approach would be to use the pull command (or pop, which works the same way in this context) to remove a random value from the source list each time one is needed:

put 1 to 1000 into available_list
put [] into unique_random_number_list
repeat 5 times
    pull any item of available_list into nextNumber
    insert nextNumber into unique_random_number_list
end repeat
log unique_random_number_list

If you’ll be using all (or most) of the values you could also just use the shuffle command to mix them up in the first place:

put 1 to 1000 into available_list
shuffle available_list

Although I think I like the pull approach if the number of values you need is relatively small and the number of items in available_list is large.

1 Like