Jump to content
IGNORED

Random numbers and percentages?


Random Terrain

Recommended Posts

Seems like a 50 percent chance is easy to figure out:
.

   temp5 = rand

   if temp5 < 128 then do something


What if you want a 10 percent chance of something happening? Would this be correct:
.

   temp5 = rand

   if temp5 < 25 then do something

.
If that is correct, then I guess this list would be too:

5% = if temp5 < 12 then do something
10% = if temp5 < 25 then do something
15% = if temp5 < 38 then do something
20% = if temp5 < 51 then do something
25% = if temp5 < 64 then do something
30% = if temp5 < 76 then do something
35% = if temp5 < 89 then do something
40% = if temp5 < 102 then do something
45% = if temp5 < 115 then do something
50% = if temp5 < 128 then do something
55% = if temp5 < 140 then do something
60% = if temp5 < 153 then do something
65% = if temp5 < 166 then do something
70% = if temp5 < 179 then do something
75% = if temp5 < 192 then do something
80% = if temp5 < 204 then do something
85% = if temp5 < 217 then do something
90% = if temp5 < 230 then do something
95% = if temp5 < 243 then do something

 

Is that list correct or all wrong?

 

 

Thanks.

Link to comment
Share on other sites

Assuming random #s coming in as values 0-255.

 

<25 - 9.765625% chance

<26 - 10.15625% chance

 

To get %age, use <# of passing possibilities> / 256 * 100

 

If you're fussy and want a more exact test then you could use multiple random samples - each bit added gives that extra precision.

 

Assuming 0-511

 

<51 - 9.9609375% chance - somewhat closer to 10% than the other two tests.

Link to comment
Share on other sites

Assuming random #s coming in as values 0-255.

 

<25 - 9.765625% chance

<26 - 10.15625% chance

 

To get %age, use <# of passing possibilities> / 256 * 100

 

If you're fussy and want a more exact test then you could use multiple random samples - each bit added gives that extra precision.

 

Assuming 0-511

 

<51 - 9.9609375% chance - somewhat closer to 10% than the other two tests.

 

Thanks. When I put "10 percent of 256" in Google, the great beast tells me the answer is "25.6" so I guess I need to be rounding up.

Link to comment
Share on other sites

Assuming random #s coming in as values 0-255.

 

<25 - 9.765625% chance

<26 - 10.15625% chance

 

To get %age, use <# of passing possibilities> / 256 * 100

 

If you're fussy and want a more exact test then you could use multiple random samples - each bit added gives that extra precision.

 

Assuming 0-511

 

<51 - 9.9609375% chance - somewhat closer to 10% than the other two tests.

 

Except that rand returns 1..255

 

Rand16 gives (something much closer to) a "perfect"

distribution (returns 0..255, with 0 coming up 255

times and all others coming up 256)

So if you really want to be accurate you'd probably

be better using rand16 which should give something

much closer to 1/256 and do it better and faster

than taking rand twice.

 

 

To get eg 10% you could scale the rand16 result

to 0-199 then look for <20.

 

If you want to be REALLY accurate there's a better

way but it requires another variable

 

you could do like this

 

rem myrand carries the bias from doing mod 10
rem forward so it gets distributed
rem rand & 15 is used to limit the range
rem so that mod 10 can be done by subtraction

myrand = (rand & 15) + myrand
if myrand >= 10 then myrand = myrand - 10
if myrand >= 10 then myrand = myrand - 10
Edited by bogax
Link to comment
Share on other sites

Except that rand returns 1..255

I thought it started at 0? Isn't that why a = (rand&1) and a = (rand/128) randomly give 0 or 1?

 

I'm using the DPC+ kernel for the thing I'm trying to finish right now, so I don't need rand16.

 

 

 

 

In my experience Rand16 does a better job of giving a truly random number.

I think that's been everybody's experience. I just wonder if anything has changed since RevEng has been improving batari Basic. Doesn't seem to be much difference between the two test programs below.

 

With regular rand:

 

random_code_with_rand.bin

 

 

With rand16:

 

random_code_with_rand16.bin

Link to comment
Share on other sites

I thought it started at 0? Isn't that why a = (rand&1) and a = (rand/128) randomly give 0 or 1?

 

I'm using the DPC+ kernel for the thing I'm trying to finish right now, so I don't need rand16.

 

 

 

 

 

I think that's been everybody's experience. I just wonder if anything has changed since RevEng has been improving batari Basic. Doesn't seem to be much difference between the two test programs below.

 

With regular rand:

 

attachicon.gifrandom_code_with_rand.bin

 

 

With rand16:

 

attachicon.gifrandom_code_with_rand16.bin

 

a = (rand&1) will return 0 for even numbers

and 1 for odd numbers.

 

a = (rand/128) will return 0 for numbers < 128

and 1 for numbers > 127

 

They both return one less 0 in a full cycle of

255 numbers so 128 1's and 127 0's for the total

of 255

 

for rand16 it'd be 32768 1's and 32767 0's

(for a full cycle of 65535)

 

 

 

Do you have the source for your binaries somewhere

so we can figure out what we're seeing?

 

Link to comment
Share on other sites

I thought it started at 0? Isn't that why a = (rand&1) and a = (rand/128) randomly give 0 or 1?

 

No, rand never returns 0-- unless you set it to 0, which breaks it (i.e., it will always return 0).

 

You can get a value of 0 in your results if you're using & or / with rand, because of the way the bits are being masked (by &) or shifted (by /)-- but rand itself won't be 0 (unless you 've broken it).

 

On the other hand, rand16 can return 0.

 

Edit: You can also get 0 if you're using other mathematical operations with rand. For example:

 

rand - 1 will return 0 through 254.

rand + 1 will return 0 through 255 except that 1 will be skipped

rand * 2 will return 0 through 254 except the odd numbers will be skipped.

Edited by SeaGtGruff
  • Like 1
Link to comment
Share on other sites

On the other hand, rand16 can return 0.

That's all I ever use when I'm not playing around with the DPC+ kernel, so I assumed rand was the same way.

 

I just did 3 tests using this:

.

   a = rand

   if a < 32 then pfpixel a 0 on

.

This one uses rand with the standard kernel:

 

random_code_with_rand_32.bin [The zero spot never gets filled in (1-255).]

 

 

This one uses rand16 with the standard kernel:

 

random_code_with_rand16_32.bin [The zero spot gets filled in (0-255).]

 

 

This one uses rand with the DPC+ kernel:

 

random_code_with_dpc_rand_32.bin [The zero spot gets filled in (0-255).]

Link to comment
Share on other sites

In case I or anybody else need this in the future, here is the updated list with rounded up numbers:

 

5% = if rand < 13 then do something
10% = if rand < 26 then do something
15% = if rand < 38 then do something
20% = if rand < 51 then do something
25% = if rand < 64 then do something
30% = if rand < 77 then do something
35% = if rand < 90 then do something
40% = if rand < 102 then do something
45% = if rand < 115 then do something
50% = if rand < 128 then do something
55% = if rand < 141 then do something
60% = if rand < 154 then do something
65% = if rand < 166 then do something
70% = if rand < 179 then do something
75% = if rand < 192 then do something
80% = if rand < 205 then do something
85% = if rand < 218 then do something
90% = if rand < 230 then do something
95% = if rand < 243 then do something

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...