1

I've come across this program but I can't seem to figure out this part. Please help me understand what is the use of rand() % 2 and how is it a condition to determine whether it's credit or debit?

#include<stdio.h>
#define NUM_TRANS 400
void * transactions(void * args) {
  int v;
    
  for(int i = 0; i < NUM_TRANS; i++) {
    v = (int) rand() % NUM_TRANS;
  
    if (rand() % 2) {
      // Crediting to user
      
     
      pthread_mutex_lock(&balance_lock);
      balance = balance + v;
      pthread_mutex_unlock(&balance_lock);
      
      pthread_mutex_lock(&credits_lock);
      credits = credits + v;
      pthread_mutex_unlock(&credits_lock);
      
    } else {
      // Debiting from user

      pthread_mutex_lock(&balance_lock);
      balance = balance - v;
      pthread_mutex_unlock(&balance_lock);
      
      pthread_mutex_lock(&debits_lock);
      debits = debits + v;
      pthread_mutex_unlock(&debits_lock);
    }
  }
}
Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
mnol
  • 23
  • 4
  • It's generating random transactions. `rand() % 2` will be `1` or `0`, `true` or `false`, to give a 50-50 decision whether the transaction will be a debit or a credit. The `v` is a random amount. – Weather Vane Jun 12 '23 at 23:03
  • The `%2` is literally "odds or evens", but it might just as well be "heads or tails", if that helps. It's a random coin flip. – Useless Jun 12 '23 at 23:06
  • 2
    The author of the code thought that `rand() % 2` is a random bit. Unfortunately, at least with the classical implementation of `rand()`, it is a _terrible_ source for random bits! The low bits returned by `rand()` are a short repeating pattern. Don't use `rand()`. Use `random()` or other random number sources. Fix this code by removing `rand()`. – Mark Adler Jun 13 '23 at 03:55
  • 1
    Does this answer your question? [Generating a random bit - lack of randomness in C rand()](https://stackoverflow.com/questions/11418113/generating-a-random-bit-lack-of-randomness-in-c-rand) – Jan Schultke Jun 13 '23 at 22:11

1 Answers1

3

The rand() % 2 is a way of generating a pseudo-random number that's either 0 or 1.

The rand() function generates a pseudo-random integer. When you take the modulus of that integer by 2 (i.e., rand() % 2), you're essentially asking for the remainder of the division of the random number by 2.

Since any number divided by 2 has a remainder of either 0 or 1, rand() % 2 will always result in either 0 or 1. This is equivalent to flipping a coin where there are only two possible outcomes.

In this program, rand() % 2 is used in an if condition to randomly decide between two different operations:

If rand() % 2 evaluates to 1 (or any non-zero number, which is considered true in C), then the program credits a user with a pseudo-random value v.

If rand() % 2 evaluates to 0 (which is considered false in C), then the program debits a user with the pseudo-random value v.

The use of a mutex lock around these operations is to ensure that no other thread can access and modify the balance, credits, and debits variables at the same time, avoiding race conditions in a multithreaded context.