Master
Posts: 7
Nickname: plut
Registered: Mar, 2002
|
|
Re: How to generate a random variable ?
|
Posted: Mar 31, 2002 7:44 AM
|
|
Hello!
I have one idea but it's not to good: (And I'm not sure I understand your question..)
You may generate a random number and check this number with numbers allowed! Ex. (pseudo code:)
// if you want to generate a number about 10, where // only 8,9,11 and 12 is ok!
int i;
while(true) // Generate number modulo 20 i = generateRandomNumber() % 20; // We want only positive numbers! if(i<0) i = i*(-1); // Check number, if true break! if(checkNumber(i)) break;
function checkNumber(i): // The number is what we want! if(i==8 || i==9 || i==11 || i==12) return true; // Return false, we dont like this number! return false;
// End pseudo code!
This code generates a positive number between 0 and 20. Then the number is checked! If it is 8,9,11 or 12 we have a winner and the while loop is terminated. This code is not to good because we generate random numbers. Because of that this code may never terminate. We may never generate the numbers 8,9,11 or 12! But cheer up. May experience tells me that as long as you work with small numbers this would work fine!
|
|