A alienware40 Member Licensed User Longtime User Oct 11, 2014 #1 Hi, I have sort of a problem when using rnd function. Rnd(0, 10) will get me any random number between 0 and 9, but can I somehow exclude for example number 7? Thanks a lot, Leon.
Hi, I have sort of a problem when using rnd function. Rnd(0, 10) will get me any random number between 0 and 9, but can I somehow exclude for example number 7? Thanks a lot, Leon.
mangojack Well-Known Member Licensed User Longtime User Oct 11, 2014 #2 there might be a more elegant way .. but for a simple fix .. B4X: Dim rndNum As Int rndNum = Rnd(1, 10) ' Create random number. 1 to 9 Do While rndNum = 7 ' Do not accept #7 rndNum = Rnd(1, 10) Loop Log (rndNum) Last edited: Oct 11, 2014 Upvote 0
there might be a more elegant way .. but for a simple fix .. B4X: Dim rndNum As Int rndNum = Rnd(1, 10) ' Create random number. 1 to 9 Do While rndNum = 7 ' Do not accept #7 rndNum = Rnd(1, 10) Loop Log (rndNum)
Straker Active Member Licensed User Longtime User Oct 11, 2014 #3 alienware40 said: Hi, I have sort of a problem when using rnd function. Rnd(0, 10) will get me any random number between 0 and 9, but can I somehow exclude for example number 7? Thanks a lot, Leon. Click to expand... B4X: Dim x As Int x=7 Do Until x<>7 x=Rnd(0,10) Loop Unfortunately B4A doesn't have a DO / LOOP UNTIL construct. If someone knows a better way to write this code, it will be appreciate. Upvote 0
alienware40 said: Hi, I have sort of a problem when using rnd function. Rnd(0, 10) will get me any random number between 0 and 9, but can I somehow exclude for example number 7? Thanks a lot, Leon. Click to expand... B4X: Dim x As Int x=7 Do Until x<>7 x=Rnd(0,10) Loop Unfortunately B4A doesn't have a DO / LOOP UNTIL construct. If someone knows a better way to write this code, it will be appreciate.
M Mahares Expert Licensed User Longtime User Oct 11, 2014 #4 @Straker: mangojack already offered the correct solution and a better way to write the code. Last edited: Oct 11, 2014 Upvote 0
Straker Active Member Licensed User Longtime User Oct 11, 2014 #5 Mahares said: @Straker: mangojack already offered the correct solution and a better way to write the code. Click to expand... Ok, I was writing the answer, and in the meantime mangojack was posting his. Upvote 0
Mahares said: @Straker: mangojack already offered the correct solution and a better way to write the code. Click to expand... Ok, I was writing the answer, and in the meantime mangojack was posting his.
derez Expert Licensed User Longtime User Oct 11, 2014 #6 B4X: x=Rnd(0,9) if x > 6 then x = x+1 Upvote 0
T Troberg Well-Known Member Licensed User Longtime User Nov 2, 2014 #7 B4X: x=Rnd(0,9) if x > 6 then x = x+1 No, no, no. That would give 7 a twice as high probability as the other outcomes. Upvote 0
B4X: x=Rnd(0,9) if x > 6 then x = x+1 No, no, no. That would give 7 a twice as high probability as the other outcomes.
mc73 Well-Known Member Licensed User Longtime User Nov 2, 2014 #8 Troberg said: B4X: x=Rnd(0,9) if x > 6 then x = x+1 No, no, no. That would give 7 a twice as high probability as the other outcomes. Click to expand... Why? From what I see in Derez's code, we have 9 outcomes (0->8) with equal probability. Adding 1 when >6, just transposes in order to avoid 7. Upvote 0
Troberg said: B4X: x=Rnd(0,9) if x > 6 then x = x+1 No, no, no. That would give 7 a twice as high probability as the other outcomes. Click to expand... Why? From what I see in Derez's code, we have 9 outcomes (0->8) with equal probability. Adding 1 when >6, just transposes in order to avoid 7.
T Troberg Well-Known Member Licensed User Longtime User Nov 3, 2014 #9 Of course, you are correct. That's what one gets for not reading properly... Upvote 0