Massassi Forums Logo

This is the static archive of the Massassi Forums. The forums are closed indefinitely. Thanks for all the memories!

You can also download Super Old Archived Message Boards from when Massassi first started.

"View" counts are as of the day the forums were archived, and will no longer increase.

ForumsCog Forum → Random numbers
Random numbers
2005-11-16, 2:24 PM #1
What's the easiest way to generate a random number in the range of 0-29?
APT 1, 2, 3/4, 5/6
TDT
DMDMD

http://veddertheshredder.com
2005-11-16, 3:29 PM #2
use the Rand() command. At least I think it's Rand()
-There are easier things in life than finding a good woman, like nailing Jello to a tree, for instance

Tazz
2005-11-16, 3:36 PM #3
That gives flex numbers in between 0.000000 and 1.000000
What I need is integers. How do I convert those flexes to whole values?
APT 1, 2, 3/4, 5/6
TDT
DMDMD

http://veddertheshredder.com
2005-11-16, 3:59 PM #4
Zeqmacaw supplied me with the answer:

dividend = rand() * 30 // 30 in my case
result = dividend - (dividend % 1)
APT 1, 2, 3/4, 5/6
TDT
DMDMD

http://veddertheshredder.com
2005-11-16, 5:04 PM #5
Cog will truncate the float anyway. If this was for an array, you could do:

Code:
// random number between 0 and 6. Will very rarely be 6.
x = y[Rand() * 6];
Historians are the most powerful and dangerous members of any society. They must be watched carefully... They can spoil everything. - Nikita Khrushchev.
Kill one man, and you are a murderer. Kill millions of men, and you are a conqueror. Kill them all, and you are a god. - Jean Rostand.
2005-11-16, 7:12 PM #6
Cog will *not* truncate a float value in a symbol. I have tested this many times. If a symbol with a float value is used in a verb that expects an int, the verb will use the truncated value, but the symbol will still keep the float value.

:)
2005-11-17, 8:22 PM #7
Which is the purpose of:

result = dividend - (dividend % 1);

Oh, and although in theory that'll give 0 to 6, in practical applications, you will NEVER see 6.
_ _ _____________ _ _
Wolf Moon
Cast Your Spell On Me
Beware
The Woods At Night
The Wolf Has Come
2005-11-18, 12:19 AM #8
rand() functions normally go from 0 inclusive to 1 non-inclusive. In other words, it NEVER returns 1. Whether cog follows these rules or not I have no idea.

The chance of getting 1 exactly (even if it doesn't follow that rule) is so small that you could simply put in a check to throw that value away if you get it and re-call the rand() function in a while() loop. The chance of having to call it more than once is either zero or infinitesimal. If it's infinitesimal, the chance of having to call it more than twice is even more (less? :p ) infinitesimal.

However, I would be VERY surprised if it did actually ever return 1. It was written by programmers after all, so they should know how rand() functions work.
2005-11-18, 3:50 AM #9
Quote:
Returns a flex, random number between 0 and 1. The random number may sometimes be 0 or 1, but it is thousands of times more likely to be inbetween. Syntax:

random=Rand();

source: Datamaster

But since I multiply the result of the rand with 30 before truncating any origional value below 0.03333... will result in 0. Thus it won't matter much, though statistically 0 will be called slightly less frequent.

1 (30) will only very rarely be called. But I only have 30 integers I'm using (0 through 29) so there is no 31st int. So in this special occasion I'll have to explicitly specify what to do.
APT 1, 2, 3/4, 5/6
TDT
DMDMD

http://veddertheshredder.com
2005-11-18, 12:06 PM #10
The following code will generate a random number between 0 and 29, with 0 and 29 being as probably as any other:

Code:
randomNumber = rand() * 29;
randomNumber = (randomNumber + 0.5) - (randomNumber + 0.5) % 1;


The first line gets a random number between 0 and 29. Since rand() generates a number between 0 and 1, multiplying the product by any number will get you a range between 0 and that number. If you wanted a range between any two numbers X and Y, use this (just for future reference):
Code:
randomNumber = rand() * (Y - X) + X;


The second line will round to the nearest integer. X % 1 will return the decimal portion of X, so if X is 22.56, it will return 0.56. In our example, let's say randomNumber is 22.56. First we add 0.5, making it 23.06. Then we get the remainder of that by doing % 1, then subtracting it from 23.06. That leaves us with 23, which was rounded up from 22.56.

Now let's say the number was 22.12. When we add 0.5, it becomes 22.62, so when we subtract the remainder, it ends up being just 22.

Not sure if you wanted an explination or not but I figured I'd offer one anyway...If you still don't get it (or if I didn't explain it well) try it out in Windows Calculator and it should become clear.
Bassoon, n. A brazen instrument into which a fool blows out his brains.
2005-11-19, 4:09 PM #11
I guess I'm missing the point. Why is it that we have to perform extra operations to remove the remainder? :rolleyes:

This is apparently for an array, so my code with an extra symbol (for that one in a thousand chance) is all you need.
Historians are the most powerful and dangerous members of any society. They must be watched carefully... They can spoil everything. - Nikita Khrushchev.
Kill one man, and you are a murderer. Kill millions of men, and you are a conqueror. Kill them all, and you are a god. - Jean Rostand.
2005-11-19, 7:02 PM #12
I don't know, mine rounds to the nearest integer, which is the more "proper" way to do it.
Bassoon, n. A brazen instrument into which a fool blows out his brains.
2005-11-20, 3:11 PM #13
I can't remember how I did it, but if you look in my Rogue Squadron CTF mod for JK -- under CTF_main.cog, you'll see that you don't even need to truncate. Rand() DOES truncate (actually, it technically doesn't need to). I believe I used something similar to this:

Code:
symbols

model    player0
model    player1
model    player2



code

.........

num = Rand() * 3;
SetPlayerModel(Player, player[rand]);


anyways, if you look in that cog you should have no troubles.
-There are easier things in life than finding a good woman, like nailing Jello to a tree, for instance

Tazz
2005-11-20, 3:28 PM #14
Ok, so it was kinda superfluous.
APT 1, 2, 3/4, 5/6
TDT
DMDMD

http://veddertheshredder.com
2005-11-20, 8:45 PM #15
Originally posted by Tazz:
Rand() DOES truncate (actually, it technically doesn't need to). I believe I used something similar to this:

Rand() just returns a random number between 0 and 1, it doesn't truncate and it doesn't not truncate. It just returns a number. It's truncated by COG when you use it as an array index.
Bassoon, n. A brazen instrument into which a fool blows out his brains.
2005-11-21, 7:51 AM #16
Emon, truncation is better than rounding here, because your mthod makes the extremes less probable than the other values. Your second line of the algorithm makes sure that 0 only happens for values in the range of 0 to smaller than 0.5, whereas e.g. 2 can happen for values in the range of 1.5 to smaller than 2.5.
I'm with Giraffe here and would use a simple rand() * 30. If it's really possible to get 1 from rand() (which I think is not the case), it will only happen once in a millenium.
"Häb Pfrässe, süsch chlepfts!" - The coolest language in the world (besides Cherokee)
2005-11-21, 5:59 PM #17
Yes, Rand() will return 1 eventually. If you don't believe me, try this out:
Code:
for(i=0; Rand() != 1; i=i+1);
PrintInt(i);

The value of i is random, but usually between 10,000 and 50,000. The lowest I got was 235, and the highest was 100,000. The practical side of this is you should check for i being 1.
Historians are the most powerful and dangerous members of any society. They must be watched carefully... They can spoil everything. - Nikita Khrushchev.
Kill one man, and you are a murderer. Kill millions of men, and you are a conqueror. Kill them all, and you are a god. - Jean Rostand.
2005-11-21, 8:06 PM #18
Well, the reality is that it works. The rand () * 30 will work. No problems, at all.
-There are easier things in life than finding a good woman, like nailing Jello to a tree, for instance

Tazz

↑ Up to the top!