You are on page 1of 16

Randomness in Optimization

Part1

Department of Computer Sciences


 Faculty of Information Technology and Computer Sciences
 Yarmouk University,  Irbid-Jordan
Randomness in Optimization
Random Solution Generation
Random Function in Optimization
Random Integer Number in Python, C++
Random Real Number in Python, C++
Random Solution Generation
Random solution generation is the generation of a
solution (a sequence of values  or symbols) that
cannot be reasonably predicted better than by
a random chance.
Random Function in Optimization
The random function (rand, rand(0,1), rnd, rd , r,
r(0,1)) in optimization may be used to generate
numbers between 0 and 1.
The random function may be used to generate
numbers between two boundaries(min and max):
value=min+(max-min) * rand(0,1)
The random function may be used to simulate
probabilities of performing actions. (Lecture5:
Randomness in Optimization Part2)
Rand in Optimization
Write a pseudo code to generate 10 real numbers
between 5 and 20?
float min=5, max=20, value
for i=1 to 10
value=min+(max-min) * rand(0,1)
print value
end for
Random Integer Number Generation in
Python
import random
random.seed(10)
print(random.random())

#0.5714025946899135
#0.5714025946899135
#0.5714025946899135
Random Integer Number
Generation in C++
#include <iostream>
using namespace std;

int main()
{
int randNum = rand();
/*Generates the same random number at each run*/
cout << randNum;
return 0;
}
Random Integer Number Generation in
Python
import random
from datetime import datetime
# Random number with system time
random.seed(datetime.now())
print(random.random())
0.4288890546751146
0.20609823213950174
0.81332125135732
0.8235888725334455
Random Integer Number
Generation in C++
#include <time.h>
#include <iostream>
using namespace std;
int main()
{
srand( time(NULL) ); //Randomize seed initialization
/*Generates a random number at each run*/
int randNum = rand();
cout << randNum;
return 0;
}
Generating Numbers in a Range in Python
import random
from datetime import datetime
random.seed(datetime.now())
for i in range(10):
print(random.uniform(1, 100))
Generating Numbers in a Range in
++C
#include <time.h>
#include <iostream>
using namespace std;
int main()
{srand( time(NULL) );
// Generates 10 random number s between 1 and 100
for (int i=0;i<10;i++){
int randNum = rand()%100+1;
cout << randNum<<endl;
}
return 0;
}
Random Integer Number in Python between
Min and Max
import random
from datetime import datetime
random.seed(datetime.now())
min1=1
max1=5
print(random. randint(min1, max1))
Random Integer Number in c++
Between Min and Max
#include <time.h>
#include <iostream>
using namespace std;
int main()
{
int max=5, min=1;
srand( time(NULL) );
// Generates a random number between min and max
int randNum = rand() % (max- min+ 1) + min;
cout<< randNum;
return 0;
}
Random real number in Python between
min and max
import random
from datetime import datetime
random.seed(datetime.now())
min1=1
max1=5
for i in range(5):
print(random.uniform(min1, max1))
Random real number in C++
between min and max
#include <time.h>
#include <iostream>
using namespace std;
int main()
{
srand( (unsigned)time( NULL ) );
double min=1, max=5, rnd, value;
// Generates 5 random number s between min and max
for (int i = 0; i < 5; i++)
{
value=min + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(max-min)));
cout << value<< endl;
}

return 0;
}
Note that the rand() function in C++  will often not be
sufficient if you need truly random numbers, the same
case is in Python.

You might also like