# Random number generator without repetition

Home Forums General Programming Random number generator without repetition

Viewing 3 posts - 1 through 3 (of 3 total)
• Author
Posts
• #116412
F0h
Participant
• Topics: 2
• Replies: 5
• Total: 7

F0h
Hello MIDIManiacs.
Sometimes you have to generate random number without repeat a value.
Cheers
F7h

Random_table = function(modulator, value)
math.randomseed(Time.getMillisecondCounterHiRes())
size = 12
Min = 1
Max = 20
Table ={}
if math.abs((Max-Min)-1)<=size then
console('Error table size')
do return end
end
--Fill the table with -1  ---
for i = 1, size do
Table = -1
end
--End fill the table---
---Number generator with no repetition---
for i = 1 , size do
flag = 1 --suppose number exits in table
while (flag == 1) do
flag = 0
r = math.random(Min , Max)
for j = 1 , i do
if Table[j] == r then
flag = 1
break
end
end
end
Table = r
end
---END Number generator with no repetition---
--Print the table---
for i = 1, size do
console(string.format('Table[%d] = %d', i, Table))
end
---end table printing--
end

#116413
F0h
Participant
• Topics: 2
• Replies: 5
• Total: 7
Random_table = function(modulator, value)
math.randomseed(Time.getMillisecondCounterHiRes())
size = 12
Min = 1
Max = 20
Table ={}
if math.abs((Max-Min)-1)<=size then
console('Error table size')
do return end
end
--Fill the table with -1  ---
for i = 1, size do
Table = -1
end
--End fill the table---
---Number generator with no repetition---
for i = 1 , size do
flag = 1 --suppose number exits in table
while (flag == 1) do
flag = 0
r = math.random(Min , Max)
for j = 1 , i do
if Table[j] == r then
flag = 1
break
end
end
end
Table = r
end
---END Number generator with no repetition---
--Print the table---
for i = 1, size do
console(string.format('Table[%d] = %d', i, Table))
end
---end table printing--
end

• This reply was modified 2 years, 6 months ago by F0h.
#116415
F0h
Participant
• Topics: 2
• Replies: 5
• Total: 7

In this example I sent CC33 to CC44 with random values.

--send CC33 ... CC44  ----
for i = 1 , size do
panel:sendMidiMessageNow(CtrlrMidiMessage(string.format('B0 %.2x %.2x', 32 + i,Table)))
end
--end

• This reply was modified 2 years, 6 months ago by F0h.
Viewing 3 posts - 1 through 3 (of 3 total)
• The forum ‘Programming’ is closed to new topics and replies.
There is currently 0 users and 69 guests online
No users are currently active
Forum Statistics
Threads: 2,495, Posts: 17,374, Members: 77,605
Most users ever online was 12 on January 22, 2019 3:47 pm