Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7594829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:31:08+00:00 2026-05-30T21:31:08+00:00

I am looking to do something somewhat evil. I want to redefine for loops

  • 0

I am looking to do something somewhat evil. I want to redefine for loops to modify the conditional. Is there any way to do this?

From the GCC documentation, redefining keywords is supported: http://gcc.gnu.org/onlinedocs/cpp/Macros.html

What I’m trying to do is make a “probabilistic” wrapper for C++, to see if I can make anything interesting with it.

#include <iostream>
#include <cstdlib>

#define P 0.85

#define RANDOM_FLOAT  ((float)rand()/(float)RAND_MAX)

#define RANDOM_CHANCE (RANDOM_FLOAT < P)

#define if(a) if((a) && RANDOM_CHANCE)

#define while(a) while((a) && RANDOM_CHANCE)


// No more for loops or do-while loops
#define do
#define goto

// Doesn't work :(
//#define for(a) for(a) if(!RANDOM_CHANCE) { break; } 


int main() {
    srand(time(NULL));

    //Should output a random list of Y's and N's
    for(int i=0; i < 100; ++i) {
        if(i < 100) {
            std::cout << "Y";
        } else {
            std::cout << "N";
        }
    }

    std::cout << std::endl;

    // Will loop for a while then terminate
    int j = 0;
    while(j < 100) {
        ++j;
        std::cout << j << "\n";
    }

   std::cout << std::endl;

   return 0;
}

A perhaps more legitimate use would be to count the number of loop iterations in a running program, for example by mapping

for(int i=0; i < 10; ++i)

to

for(int i=0; i < 10; ++i, ++global_counter)

Is it possible to accomplish what I am trying to do?


EDIT: Thanks for the responses – I really appreciate it!

One of the things I can do with this is simulate a fair coin (effectively force P to be 0.5), by noting that if you have a biased coin, Pr(HT) == Pr(TH). I didn’t discover this trick, but it’s useful. So that means that you can approximate any probability distribution for P.

bool coin() {
  bool c1 = false;
  bool c2 = false;

  if(true) {
    c1 = true;
  }
  if(true) {
    c2 = true;
  }

  //If they have different faces.
  bool valid = (c1 && !c2) || (!c1 && c2);
  bool result = c1;

  if(valid) { return result; }

  return coin();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:31:10+00:00Added an answer on May 30, 2026 at 9:31 pm

    Have you tried adding an else to your for loop? That should make it work.

    #define for(a) for(a) if(!RANDOM_CHANCE) { break; } else
    
    for (int i = 0; i < 10; ++i)
    {
        // do something
    }
    
    /* should roughly compile to:
    for (int i = 0; i < 10; ++i) if(!RANDOM_CHANCE) { break; } else
    {
        // do something
    }
    
    or (differing only in whitespace):
    
    for (int i = 0; i < 10; ++i)
        if(!RANDOM_CHANCE)
        {
            break;
        }
        else
        {
            // do something
        }
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for something like CodeIgniter's: $this->db->last_query(); ( http://codeigniter.com/user_guide/database/helpers.html )
I'm looking for something similar to the table creation form from phpmyadmin, but it
I'm not sure if I am approaching this the right way; looking for some
Can someone recommend a simple c# code generator. I just looking something with methods
Looking for something like: $(input:radio:checked).previous(name, original_name).attr(name,new_name); I tried a few different seen around here
Looking for something similar to xerces for parsing an xml file in ruby. I
Im looking for something like a search and replace functionality in Solr. I have
I'm looking for something that works in PHP and is similar to crystal reports.
I'm looking for something (preferably a Firefox extension) which will allow me to see
I'm looking for something to turn my C# XML comments into documentation. I don't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.