abram
New Member
Posts: 44
|
Post by abram on Oct 4, 2023 0:03:58 GMT
I was thinking about random numbers and how if you want a random number in basic it is always do INT(RND*number)+1. I was wondering if this actually produces a random number that you intend. I wanted a random number 1 or 0. So I plugged in the number 1. I don't think there will be a *general* 50/50 *chance* of getting 1 or 0; with the INT(RND*number)+1 method. I think the number would be 1 -- 1/10 of the time and the number would be 0 -- 9/10 of the time. The more I thought about it the more it didn't make sense.
I think if you use INT(RND * 10) MOD 2. You are more likely to get a 1 or a 0 in a 50/50 _chance_.
Does this make sense?
|
|
|
Post by bplus on Oct 4, 2023 0:23:33 GMT
Here's how I'd do 0 or 1 ' If you want only 0 or 1 then you could go
For i = 1 To 100000 If Rand01% = 1 Then one = one + 1 Else zero = zero + 1 Print "zeros:"; zero, " ones:"; one Next
Function Rand01% If Rnd < .5 Then Rand01% = 1 ' else it's 0 End Function abram This: r& = INT(RND*number)+1 is good for an integer from 1 to Number And yes this works fine! Randomize Timer For i = 1 To 100000 If Int(Rnd * 10) Mod 2 Then one = one + 1 Else zero = zero + 1 Print "zeros:"; zero, " ones:"; one Next
|
|
|
Post by bplus on Oct 4, 2023 1:44:49 GMT
ZXDunny (developer of SpecBAS) has suggested this:
Randomize Timer For i = 1 To 100000 If Int(Rnd + .5) Then one = one + 1 Else zero = zero + 1 Print "zeros:"; zero, " ones:"; one Next
|
|
abram
New Member
Posts: 44
|
Post by abram on Oct 4, 2023 2:11:07 GMT
I found something that works.
int(Rnd * 2)
it is like the if then statement with the .5 value. all it does is multiply all the numbers less than one by 2. If the number is .5 or greater it produces 1. if it is smaller than .5 it produces a zero
|
|
|
Post by bplus on Oct 4, 2023 8:11:08 GMT
Yes!
The way I read Rnd * Number = 0 to Number - .0000000001 (some tiny amount) So for number = 2 it is 0 to 1.9999999999 So Int( of that ) is 0 or 1 almost equally.
|
|
|
Post by bplus on Oct 4, 2023 9:39:35 GMT
Go left -1 or right +1 but you can't stay here: 2 * Int(Rnd * 2) - 1
Test
For i = 1 To 10000 'Print 2 * Int(Rnd * 2) - 1;
If 2 * Int(Rnd * 2) - 1 = 1 Then one = one + 1 Else neg = neg + 1 Print one, neg Next
|
|
abram
New Member
Posts: 44
|
Post by abram on Oct 11, 2023 23:55:43 GMT
what is 0 MOD 2?
|
|
|
Post by bplus on Oct 12, 2023 11:06:47 GMT
0, all even numbers mod 2 are 0; that's a good test for an even number. All odd numbers mod 2 are 1; a test for odd numbers. (All numbers a multiple n) mod n are 0; a test if n divides a number ie if m mod n = 0 then m is multiple of n 3,6,9,12,15,18.... mod 3 = 0
|
|
abram
New Member
Posts: 44
|
Post by abram on Oct 14, 2023 13:03:43 GMT
What command rounds up if the number is greater than half? Do I have to make an if statement?
Dim a As Double Dim b As Double Dim c As Double Dim d As Double Dim e As Integer Dim f As Integer a = 2.7 b = 2.4 c = a d = b e = a f = b Print "A ="; a, "B ="; b Print "C ="; c, "D ="; d Print "INT C ="; Int(c), "INT D ="; Int(d) Print "E ="; e, "F ="; f
***OUTPUT*** A = 2.7 B = 2.4 C = 2.7 D = 2.4 INT C = 2 INT D = 2 E = 3 F = 2
From the output of my test code it appears that if you have an integer type that it will round up. Is this the correct way?
|
|
|
Post by bplus on Oct 14, 2023 13:40:08 GMT
You can round up to an Integer like this RoundUp% = INT(Sing! + .5) ' this is for positive floats
If you want to do positives up and negs down try this: if sing! < 0 then RoundUp% = INT(Sing! - .5) else RoundUp% = INT(Sing! + .5)
There also is a function called _Round()
|
|