d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Probability In C
123Next
Add Reply New Topic New Poll
Member
Posts: 1,753
Joined: Apr 12 2011
Gold: 0.00
Mar 25 2013 01:22am
say i want a function to return 0 with a 10% chance. how would i emulate that probability?
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 25 2013 03:11am
Quote (Reverence @ Mar 25 2013 03:22am)
say i want a function to return 0 with a 10% chance.  how would i emulate that probability?


Code
int probability()
{
  srand(time(NULL));
  return rand()%10;
}
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Mar 25 2013 08:00am
if you want to call that function very fast after you already called it, it may be better to "srand" it only once. because if you call the function faster than your time function gives a fresh result, the result won't be that "random". ofc it doesnt affect the long-term-probability of 10%, but it definately is "more random".
Member
Posts: 1,753
Joined: Apr 12 2011
Gold: 0.00
Mar 25 2013 12:23pm
Quote (AbDuCt @ Mar 24 2013 11:11pm)
Code
int probability()
{
  srand(time(NULL));
  return rand()%10;
}


could you explain what those functions do individually and what that does as a whole?
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Mar 25 2013 12:45pm
first type "man 3 rand" in console and read the output: (or use google... seriously if you cant find out such simple stuff without posting in a forum, you will have days for a simple program)
Code
The rand() function returns a pseudo-random integer in the range 0 to
RAND_MAX inclusive (i.e., the mathematical range [0, RAND_MAX]).

The srand() function sets its argument as the seed for a new sequence
of  pseudo-random integers to be returned by rand().  These sequences
are repeatable by calling srand() with the same seed value.

If no seed value is provided, the rand()  function  is  automatically
seeded with a value of 1.


% is the modulo operator
Member
Posts: 1,753
Joined: Apr 12 2011
Gold: 0.00
Mar 25 2013 01:24pm
of course i found that description, but i'm trying to understand how they work - to learn why it's not good to use rand() on its own, what the significance of the seed is, etc.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 25 2013 01:41pm
Quote (Reverence @ Mar 25 2013 03:24pm)
of course i found that description, but i'm trying to understand how they work - to learn why it's not good to use rand() on its own, what the significance of the seed is, etc.


if you are doing cryptography or anything else that needs randomness you are better off reading from /dev/random or /dev/urandom. those are files that are very random and created by hardware instead of software.

rand() can actually be predicted if you know the initial seed value.
Member
Posts: 1,753
Joined: Apr 12 2011
Gold: 0.00
Mar 25 2013 02:04pm
Quote (AbDuCt @ Mar 25 2013 09:41am)
if you are doing cryptography or anything else that needs randomness you are better off reading from /dev/random or /dev/urandom. those are files that are very random and created by hardware instead of software.

rand() can actually be predicted if you know the initial seed value.


i'm actually just recreating a game ( got inspired after seeing your post in chd yesterday lol) so the code you posted is perfect
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 25 2013 02:08pm
Quote (Reverence @ Mar 25 2013 04:04pm)
i'm actually just recreating a game ( got inspired after seeing your post in chd yesterday lol) so the code you posted is perfect


heh. im still trying to figure out how to present the next part. its easy enough commenting code and explaining it over all but the methods of finding what i needed to know in order to create the code (ex grabbing memory addresses/pointers/addresses of machine code) is beyond what screenshots and text and do really.
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Mar 25 2013 03:58pm
rand() gives a "random" number.
but if you restart the game, the exyctly same random number will be given by rand() again.
so you would play the same exact game multiple times... wouldn't be that funny...
thats why you need a "seed".
you can set it with srand().
so if you write srand(2) at the start of the game, it will be different than the game, where you write srand(1) at the start of the game.
but still you got the problem, that the games with the same number 'x' inside of srand(x) will be the same game as srand(x).
so if you put inside srand() the time, the games will be different of each other, if the time-function's result changes between the games.

the function rand() repeats every xxxxxxxxxx (i dont know how long the sequence actually is)
thats why it's also called a pseuro random generator
with the right math you could get the state of this random generator.
with the right state, you can predict the 'future' states of the generator.
but you need enough data for that.
you can decrypt WEP-encrypted-wifis with that....

so to your problem again, you get a number between 1 and a huge number. if you make modulo 10, you get numbers between 0 and 9. you only get hte 0 in one case of the 10 possible cases, thats where the 10% come from.


Go Back To Programming & Development Topic List
123Next
Add Reply New Topic New Poll