Showing posts with label random. Show all posts
Showing posts with label random. Show all posts

3/14/2023

python dict shuffle

shuffle dict order 


.

import random

my_dict = {'apple': 2, 'banana': 3, 'orange': 1, 'kiwi': 4}

# Convert the dictionary to a list of tuples and shuffle it
items = list(my_dict.items())
random.shuffle(items)

# Convert the shuffled list back to a dictionary
shuffled_dict = {k: v for k, v in items}

print(shuffled_dict)

..


Thank you.


9/24/2022

torch np cuda seed random everything

 

refer to below code:


..

import numpy as np
import os
import random
import torch

SEED = 42

def seed_everything(seed):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True

seed_everything(SEED)

..


Thank you.

www.marearts.com

9/15/2018

python 1 line code for N random number (tip)

Make 1~100 random integer as 10 loops


import random
myr = [random.randrange(1, 100) for _ in range(10)]
print(myr)

result
[9, 67, 32, 55, 79, 47, 86, 62, 95, 30]

Thank you. ^^


11/03/2014

opencv randn(...) example

opencv randn is like in matlab.

The randn function make values of normal distribution random

in matlab
randn is usage like this..

randn()
>> 0.4663

randn(10,1)'
>>   -0.1465    1.0143    0.4669    1.5750   -1.1900    0.2689   -0.2967   -0.4877    0.5671    0.5632

to use mean 5, variance 3
5+3*rand(10,1)
>> 6.2932   12.5907    6.6214    1.6941    4.8522    3.1484    6.1745    4.5230    5.2183    5.6888


OK, now consider case of OpenCV
We will make mean 10 and variance 2 normal distribution random values and fill in 2x10 matrix.

example 1)
randn
..
cv::Mat matrix2xN(2, 10, CV_32FC1);
randn(matrix2xN, 10, 2);
for (int i = 0; i < 10; ++i)
{
  cout << matrix2xN.at<float>(0, i) << " ";
  cout << matrix2xN.at<float>(1, i) << endl;
}
..

example 2)
randn and randu
..
cv::Mat matrix2xN(2, 10, CV_32FC1);
randn(matrix2xN, 10, 2);
for (int i = 0; i < 10; ++i)
{
    cout << matrix2xN.at< float>(0, i) << " ";
    cout << matrix2xN.at< float>(1, i) << endl;
}

//gaussian generation example
Mat Gnoise = Mat(5, 5, CV_8SC1);
randn(Gnoise, 5, 10); //mean, variance
cout << Gnoise << endl;
//
Mat Unoise = Mat(5, 5, CV_8SC1);
randu(Unoise, 5, 10); //low, high
cout << Unoise << endl;


//noise adapt
Mat Gaussian_noise = Mat(img.size(), img.type());
double mean = 0;
double std = 10;
randn(Gaussian_noise, mean, std); //mean, std
Mat colorNoise = img + Gaussian_noise;
..

7/29/2014

no duplication random, (stl random_shuffle example source code)

sometimes, we need no duplication random sequence data.
simple method is using shuffle random in stl.

refer to this source code.

#include < stdio.h>
#include < vector>
#include < algorithm>
#include < cstdlib>
#include < ctime>
#include < iostream>
using namespace std;


void main()
{
 //set size and initialize
 vector< int > A(10);
 for(int i=0; i< A.size(); ++i)
  A[i] = i;

 //confirm
 printf("----origin data \n");
 for(int i=0; i< A.size(); ++i)
  printf("[%d] - %d \n", i, A[i] );
 printf("----\n");

 //random 
 srand( unsigned (time(0) ) );
 random_shuffle( A.begin(), A.end() );

 //confirm
 printf("---- After shuffle \n");
 for(int i=0; i< A.size(); ++i)
  printf("[%d] - %d \n", i, A[i] );
 printf("----\n");
}


..