fork download
  1. //
  2. // main.cpp
  3. // Custom Randomize
  4. //
  5. // Created by Himanshu on 27/11/22.
  6. //
  7.  
  8. #include <iostream>
  9. using namespace std;
  10.  
  11. int randomize() {
  12. int x = rand()%5 + 1;
  13. return x;
  14. }
  15.  
  16. int customRandomize() {
  17. int N = 5*randomize() + randomize() - 5;
  18.  
  19. while (N > 7) {
  20. N = 5*randomize() + randomize() - 5;
  21. }
  22.  
  23. return N;
  24. }
  25.  
  26. //This method is slightly more efficient
  27. int customRandomize2 () {
  28. int N = 5*randomize() + randomize() - 5;
  29.  
  30. while (N > 21) {
  31. N = 5*randomize() + randomize() - 5;
  32. }
  33.  
  34. return ((N%7) + 1);
  35. }
  36.  
  37. int main () {
  38.  
  39. int x, y;
  40.  
  41. cout<<"Randomly generated integers between 1 and 7:"<<endl;
  42. for (int i=0; i<5; i++) {
  43. x = customRandomize();
  44. cout<<x<<endl;
  45. y = customRandomize2();
  46. cout<<y<<endl;
  47. }
  48.  
  49. return 0;
  50. }
Success #stdin #stdout 0.01s 5436KB
stdin
Standard input is empty
stdout
Randomly generated integers between 1 and 7:
5
4
2
6
1
4
6
3
7
2