Here is some code... I'm not sure if it will work but it will give you a good start.
XPathNavigator root = MainDataSource.CreateNavigator();
for (int i=0; i<10; i++)
{
Random random = new Random();
int question = random.Next(1, 40);
string value = root.selectsinglenode("[YourXPath]/my:HideQuestion" + question, NamespaceManager).Value;
if(value == "0") //or value == "false"
{
root.selectsinglenode("[YourXpath]/my:HideQuestion" + question, NamespaceManager).setValue("1");//or "true"
}
else
{
i--;
}
}
Where you see [YourXpath], it is the xpath of the control you want to access to. To have it, you need to select your control (in infopath), and go to the "Data Source" Panel. The data related to the control will be highlighted. Right click on it and select "Copy XPath". Then you just have to paste it in the code.
If you have any other question, don't hesitate to ask!
Hope that helps!
Edit : This code is C# by the way