Generate random number with repeat rule

Asked

Viewed 177 times

0

I need an SQL command that generates a random number between 1 and 11, and that for number 1 is generated more often than others.

Currently I use the following command:

FLOOR(1 + (RAND() * 11))

It is in a WHERE command of SQL, example:

WHERE numero < FLOOR(1 + (RAND() * 11))

This WHERE is in an SQL command, which is executed in a precedent and runs every second, that is, every second is generated a different number.

The problem is that this command generates the number randomly, without the rule of the number 1 more frequently.

How can I change this command so that it generates the numbers randomly and the number 1 is generated more frequently?

  • How often do you want the number 1 to repeat itself?

  • @Felipemarinho could be 2 times followed, or each two different numbers generated.

  • Try something like FLOOR(1 + (POWER(RAND(),2) * 11))

  • @Motta even worked, but did not generate the number 1 very often not. He appeared more often, but not as needed. It appears every 5 or 6 numbers generated..

  • Increase the exponent to 3 for example , another solution and create a Function , assemble an array with the frequency you want and sort the position of this array , something like 1,1,1,1,1,1,1,1,1,1,2,3,4,5,6,7,8,9,10,11 20 positions being the frequent 1 50% of the time

  • @Motta I don’t know much about SQL, who set up this process for me was someone else I don’t have any more contact with. What would this array look like? And in the case of the exponent, it’s that number 2 that you put there?

  • FLOOR(1 + (POWER(RAND(),3) * 11)) changing this exponent until adjusting the required bias. Just so you understand POWER raises a number to a power for values between 0 and 1 to rise to a number greater than 1 "achata" so to speak , 0.5 2 = 0.25 , for example , this makes the lowest values more frequent in sampling, simulating . here with 3 1 got 45% of occurrences

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.