public void FindPrimeNumber() { int n = (RND.Next() % 16) + 1000; int count = 0; int a = 2; while (count < n) { long b = 2; int prime = 1; // to check if found a prime while (b * b <= a) { if (a % b == 0) { prime = 0; break; } b++; } if (prime > 0) { count++; } a++; } result = --a * 333; //the result can be processed }
public static string Generate(int length) { string token = ""; while (token.Length < length) { int bottleFlip = RND.Next(0, 3); // 0 = number (0-9) // 1 = upper (A-Z) // 2 = lower (a-z) if (bottleFlip == 0) { token += RND.Next(0, 10).ToString(); } else if (bottleFlip == 1) { int id = RND.Next(65, 91); char chr = Convert.ToChar(id); token += chr.ToString(); } else { int id = RND.Next(97, 123); char chr = Convert.ToChar(id); token += chr.ToString(); } } return(token); }
public Thief(string name) { stat_strength = RND.Next(2, 9); stat_intelligence = RND.Next(2, 9); stat_agility = RND.Next(8, 17); myname = name; }
IEnumerator <TaskContract> FindPrimeNumber() { while (true) { int n = (RND.Next() % 16) + 1000; int count = 0; int a = 2; while (count < n) { long b = 2; int prime = 1; // to check if found a prime while (b * b <= a) { if (a % b == 0) { prime = 0; break; } b++; } if (prime > 0) { count++; } a++; } yield return(--a * 333); //the result can be processed } }
static void Random() { Array = new int[60]; for (int i = 0; i < Array.Length; i++) { Array[i] = RND.Next(100); } }
public Macroblock(World GW) { this.Size = ChunkSize; this.ShaderProg = GlobalShaderProg; this.Atlas = GlobalAtlas; this.GW = GW; Clr = new Vector3((float)RND.Next(0, 100) / 100, (float)RND.Next(0, 100) / 100, (float)RND.Next(0, 100) / 100); ChunkMatrix = Matrix4.Identity; }
public static T Pick <T>(this IEnumerable <T> list, Random RND = null) { if (RND == null) { RND = new Random(); } List <T> L = new List <T>(); foreach (var item in list) { L.Add(item); } return(L[RND.Next(L.Count)]); }
public RedEnemy() { try { for (int i = 0; i < 8; i++) { images.Add(new PictureBox()); images[i].Image = new Bitmap($"frame_{i}_delay-0.1s.png"); } } catch { MessageBox.Show("Pictures must be in the same folder as .EXE"); } mainImage.Image = images[0].Image; mainImage.SizeMode = PictureBoxSizeMode.AutoSize; hp = 400; yPosition = RND.Next(45, pForm.Height - mainImage.Height - 65); }
/// <summary> /// Method for generating random value of given range /// </summary> /// <returns> /// Random value from given range /// </returns> public override object GenerateRandom() { return(RND.Next(RandomMin, RandomMax)); }
public static double[] stratmc(Func <vector, double> f, double[] a, double[] b, int npoints, bool print_points, Random RND = null) { if (RND == null) { RND = new Random(1); } int dim = a.Length; int min_points = 8 * dim; int min_new_points = 4 * min_points; double volume = 1; for (int i = 0; i < dim; i++) { volume *= (b[i] - a[i]); } var x = new vector(dim); vector av = new vector(a); vector bv = new vector(b); if (npoints <= 2 * min_points)//plain MC { int n1 = npoints / 2; int n2 = npoints - n1; (double r1, double e1) = plainmc(f, av, bv, n1, print_points, RND); (double r2, double e2) = plainmc(f, av, bv, n2, print_points, RND); double mcinteg = (n1 * r1 + n2 * r2) / (n1 + n2); double mcerror = Abs(r1 - r2) / 4; return(new double[] { mcinteg, mcerror }); } int trial_points = (int)Max(min_points, trial_fraction * npoints); int new_points = npoints - trial_points; var aL = new vector(dim); var aR = new vector(dim); var vL = new vector(dim); var vR = new vector(dim); var nL = new int[dim]; var nR = new int[dim]; var sum_f_L = new vector(dim); var sum_f2_L = new vector(dim); var sum_f_R = new vector(dim); var sum_f2_R = new vector(dim); for (int n = 0; n < trial_points; n++)// trial run { randomx(x, av, bv, RND, print_points); double fx = f(x); for (int i = 0; i < dim; i++) // sub-sums { if (x[i] < (a[i] + b[i]) / 2) { nL[i]++; sum_f_L[i] += fx; sum_f2_L[i] += fx * fx; } else { nR[i]++; sum_f_R[i] += fx; sum_f2_R[i] += fx * fx; } } } double vmax = 0; int i_bisect = RND.Next(0, dim); for (int i = 0; i < dim; i++) { if (nL[i] > 0) { aL[i] = sum_f_L[i] / nL[i]; vL[i] = sum_f2_L[i] / nL[i] - Pow(aL[i], 2); } if (nR[i] > 0) { aR[i] = sum_f_R[i] / nR[i]; vR[i] = sum_f2_R[i] / nR[i] - Pow(aR[i], 2); } double measure = Abs(aL[i] - aR[i]); if (measure > vmax) { vmax = measure; i_bisect = i; } } double weight_L = vL[i_bisect]; double weight_R = vR[i_bisect]; if (weight_L == 0 && weight_R == 0) { weight_L = weight_R = 1; } int new_points_L = min_points + (int)Round((new_points - 2 * min_points) * weight_L / (weight_L + weight_R)); int new_points_R = min_points + (int)Round((new_points - 2 * min_points) * weight_R / (weight_L + weight_R)); if (new_points <= min_new_points) { new_points_L = new_points / 2; new_points_R = new_points - new_points_L; } var aa = (double[])a.Clone(); aa[i_bisect] = (a[i_bisect] + b[i_bisect]) / 2; var bb = (double[])b.Clone(); bb[i_bisect] = (a[i_bisect] + b[i_bisect]) / 2; double[] result_L = stratmc(f, a, bb, new_points_L, print_points, RND); double[] result_R = stratmc(f, aa, b, new_points_R, print_points, RND); double new_integ = result_L[0] + result_R[0]; double new_error = Sqrt(Pow(result_L[1], 2) + Pow(result_R[1], 2)); double trial_average = (sum_f_L[i_bisect] + sum_f_R[i_bisect]) / trial_points; double trial_integ = trial_average * volume; double trial_error = Abs(trial_integ - new_integ) / 4; double integ = (trial_integ * trial_points + new_integ * new_points) / (trial_points + new_points); double error2 = (Pow(new_error * new_points, 2) + Pow(trial_error * trial_points, 2)) / Pow(new_points + trial_points, 2); double error = Sqrt(error2); return(new double[] { integ, error }); }
public static double[] miser (Func<vector,double> f, double[] a, double[] b, int npoints, Random RND=null) { if(RND==null)RND=new Random(1); int dim=a.Length; int min_points=8*dim; int min_new_points=4*min_points; double volume=1; for(int i=0;i<dim;i++) volume*=(b[i]-a[i]); var x =new vector(dim); if(npoints<=2*min_points){//plain MC /* return plain(f,a,b,npoints,RND); */ int n1=npoints/2; int n2=npoints-n1; double[] r1=plain(f,a,b,n1,RND); double[] r2=plain(f,a,b,n2,RND); double mcinteg=(n1*r1[0]+n2*r2[0])/(n1+n2); double mcerror=Abs(r1[0]-r2[0])/4; return new double[] {mcinteg,mcerror}; } int trial_points=(int)Max(min_points,trial_fraction*npoints); int new_points=npoints-trial_points; var aL=new vector(dim); var aR=new vector(dim); var vL=new vector(dim); var vR=new vector(dim); var nL=new int[dim]; var nR=new int[dim]; var sum_f_L=new vector(dim); var sum_f2_L=new vector(dim); var sum_f_R=new vector(dim); var sum_f2_R=new vector(dim); for(int n=0;n<trial_points;n++){// trial run randomx(x,a,b,RND); double fx=f(x); for(int i=0;i<dim;i++){ // sub-sums if(x[i]<(a[i]+b[i])/2){nL[i]++;sum_f_L[i]+=fx;sum_f2_L[i]+=fx*fx;} else {nR[i]++;sum_f_R[i]+=fx;sum_f2_R[i]+=fx*fx;} } } double vmax=0;int i_bisect=RND.Next(0,dim); for(int i=0;i<dim;i++){ if(nL[i]>0){ aL[i]=sum_f_L[i]/nL[i]; vL[i]=sum_f2_L[i]/nL[i]-Pow(aL[i],2); } if(nR[i]>0){ aR[i]=sum_f_R[i]/nR[i]; vR[i]=sum_f2_R[i]/nR[i]-Pow(aR[i],2); } double measure=Abs(aL[i]-aR[i]); //double measure=vL[i]+vR[i]; //double measure=Max(vL[i],vR[i]); if(measure>vmax){vmax=measure;i_bisect=i;} } double weight_L=vL[i_bisect]; double weight_R=vR[i_bisect]; if(weight_L==0 && weight_R==0)weight_L=weight_R=1; int new_points_L=min_points +(int)Round((new_points-2*min_points)*weight_L/(weight_L+weight_R)); int new_points_R=min_points +(int)Round((new_points-2*min_points)*weight_R/(weight_L+weight_R)); if(new_points<=min_new_points){ new_points_L=new_points/2; new_points_R=new_points-new_points_L; } var aa = (double[])a.Clone();aa[i_bisect]=(a[i_bisect]+b[i_bisect])/2; var bb = (double[])b.Clone();bb[i_bisect]=(a[i_bisect]+b[i_bisect])/2; double[] result_L = miser(f,a,bb,new_points_L,RND); double[] result_R = miser(f,aa,b,new_points_R,RND); double new_integ=result_L[0]+result_R[0]; double new_error=Sqrt(Pow(result_L[1],2)+Pow(result_R[1],2)); double trial_average=(sum_f_L[i_bisect]+sum_f_R[i_bisect])/trial_points; double trial_integ=trial_average*volume; /* double trial_variance=(sum_f2_L[i_bisect]+sum_f2_R[i_bisect])/trial_points -trial_average*trial_average; double trial_error=trial_variance*volume/Sqrt(trial_points); */ double trial_error=Abs(trial_integ-new_integ)/4; double integ=(trial_integ*trial_points+new_integ*new_points)/ (trial_points+new_points); double error2=(Pow(new_error*new_points,2)+Pow(trial_error*trial_points,2))/ Pow(new_points+trial_points,2); double error=Sqrt(error2); //return new double[] {integ,Abs(trial_integ-new_integ)}; return new double[] {integ,error}; //return new double[] {integ,new_error}; //return new double[] {new_integ,new_error}; }//miser
private bool _generateRandomBool() { return(RND.Next(0, 1) == 1); }
/// <summary> /// Method for generating random value of given range /// </summary> /// <returns> /// Random value from given range /// </returns> public override object GenerateRandom() { return(Convert.ToUInt32(RND.Next(Convert.ToInt32(RandomMin), Convert.ToInt32(RandomMax)))); }
// читает вопрос из файла теста private Boolean getQw() { countAns = 0; // считываем тэг <q> xmlReader.Read(); if (xmlReader.Name == "q") { // здесь прочитан тэг <q>, // атрибут text которого содержит вопрос, а // атрибут src - имя файла иллюстрации. // id = Convert.ToInt32(xmlReader.GetAttribute("id")); // извлекаем значение атрибутов: qwet = xmlReader.GetAttribute("text"); pic = xmlReader.GetAttribute("src"); if (!pic.Equals(string.Empty)) { pic = fpath + pic; } // входим внутрь узла xmlReader.Read(); int i = 0; // считываем данные узла вопроса <q> while (xmlReader.Name != "q") { xmlReader.Read(); // варианты ответа if (xmlReader.Name == "a") { // запоминаем правильный ответ if (xmlReader.GetAttribute("right") == "yes") { ro = i.ToString();//запоминаем номер ответа и переводим его в строку right = i; // MessageBox.Show(ro); } // считываем вариант ответа xmlReader.Read(); if (!(String.IsNullOrEmpty(ro))) //если ответ правильный считан { sright = xmlReader.Value; //запонминаем правильный ответ // MessageBox.Show(sright); } ro = ""; answ.Add(xmlReader.Value); // выходим из узла <a> xmlReader.Read(); countAns++; i++; } for (int k = 0; k < answ.Count; k++) { string tmp = answ[0]; answ.RemoveAt(0); answ.Insert(RND.Next(answ.Count), tmp); } q = new test(qwet, answ); //qwetions.Add(q); } // выходим из узла вопроса <q> xmlReader.Read(); return(true); } // если считанный тэг не является // тэгом вопроса <q> else { return(false); } }
public int GenerateDie() => RND.Next(1, Constants.DIE_MAX + 1);
public void GenerateRONumber( ) { RONumber = RND.Next(100000, 999999); }
static public int Rnd(int _min, int _max) { return(RND.Next(_min, _max)); }