1

I've been trying to figure out a way to generate a random integer number in C within a range of 0 to 1 (for true/false values). I'm not sure if there is a way to do that.

I've tried to manually program the random number function with for and while loops

  int randomNumber;
  for (randomNumber = 0; randomNumber<=1; randomNumber++) {
      randomNumber = rand() % 100 + 1;
      printf("%d\n", randomNumber);
  }

I expect it to print out a random number (still analyzing this so not sure what it would print out), but I need the basis to getting the 0 and 1 for the true/false values.

  • 4
    If `x % 100` gives you a number in the range `[0, 99]`, what would give you a number in the range `[0, 1]`? – EOF Aug 25 '19 at 06:51
  • 1
    @EOF well, yes, you can use `% 2` and pretend it will be what you want. – Antti Haapala -- Слава Україні Aug 25 '19 at 07:13
  • @AnttiHaapala Or you can look at the question and pretend that it asks what the supposed duplicate question asks. – EOF Aug 25 '19 at 07:18
  • 1
    "I've been trying to figure out a way to generate a random integer number in C within a range of 0 to 1 (for true/false values). I'm not sure if there is a way to do that." It is quite clear what the question asks. It asks for that. Then your supposed answer is wrong. – Antti Haapala -- Слава Україні Aug 25 '19 at 07:19
  • 1
    @AnttiHaapala Not sure what you're seeing when you look at this question, but I definitely don't see any hint that it has anything to do with any perceived problem in the quality of randomness of low-order bits in `rand()` on the OP's C implementation. I'd go so far as to say that you are imagining any such indication you may perceive in the question. – EOF Aug 25 '19 at 07:26
  • 1
    The question was "how do I generate a random boolean". That it supposedly happens by modifying this code is besides the point. – Antti Haapala -- Слава Україні Aug 25 '19 at 07:28
  • @AnttiHaapala I'm kinda running out of AGI here. It's obvious that you're trying to find a justification for your incorrect duplicate. You marked this a duplicate of the question you *wanted to see*, not the one that was asked. Also, not *@* tagging me in your answers to have the last word is somewhat odd. – EOF Aug 25 '19 at 07:33
  • 1
    @EOF notice that the **question** already includes the "obvious" answer and the **answer** tells how it is wrong and what is the correct answer. This is **the** very correct duplicate. – Antti Haapala -- Слава Україні Aug 25 '19 at 07:36

0 Answers0