- Kernel class Random (not visible in AOT). Generates a value in the range [0..32767] by calling nextInt() instance method.
- Class RandomGenerate (which extends Random class). Generates a value in the specified range by calling randomInt() instance method.
- xGlobal::randomPositiveInt32() method. Generates a value in the range [0..2147483647].
Actually, the third way seems to be the most preferable because there is some trick with first two ones. If the new Random class instance will be created each time before calling nextInt(), the output values will be very far from random. For example, the following code:
static void testRandom(Args _args)
{
Random random;
int i;
for (i = 0; i < 3; i++)
{
random = new Random();
info(int2str(random.nextInt()));
}
}
On the first run will give the following output:
30800
30800
30800
And on the second run:
30810
30810
30810
As a fourth option you could use System.Random.
ReplyDeleteYou can specify a seed in the new to get better results. You can also get random Double or Byte values.
Daily useful things. Great idea. Will follow it with interest.
ReplyDelete/Jonas
2 Chrono:
ReplyDeleteI would recommend to avoid using System.Random.
It will add a rather large overhead compared to the use of X++ functions.
It is indeed slower. However, for it to be actually noticeable you would have to be generating thousands and thousands of random numbers. :p
ReplyDeleteThanks for letting me know though.
I just tried method #2 to generate 10,000 unique numbers using a range of 1,000,000-9,999,999.
ReplyDeleteIt performed about 12,000 operations to get 10,000 "unique" numbers. They were all FAR from unique however.
**I meant to say, far from random. They looked almost sequential.
ReplyDeleteThe 3rd option for me was the best way to generate a large group of random numbers. The first two just ends up being sequential. I wrote a little blog post inspired by this:
ReplyDeletehttp://alexondax.blogspot.com/2011/04/generating-better-random-numbers-with.html
Well, you are right about odd behavior of the option #2 with ranges that are outside [0, 32767] range. The implementation of the randomInt() method is bad.
ReplyDeleteTo get random numbers in the range I suggest the following algo:
n = xGlobal::randomPositiveInt32() mod (9999999 - 1000000) + 1000000;
Hello, its nice article on the topic of media print, we all be familiar
ReplyDeletewith media is a fantastic source of facts.
Visit my weblog; airplanes games
nike trainers, longchamp, abercrombie and fitch, ralph lauren, longchamp, polo lacoste, polo ralph lauren, nike tn, michael kors, timberland, converse pas cher, nike blazer, ray ban sunglasses, michael kors, air max, nike air max, sac louis vuitton, barbour, nike free, north face, vans pas cher, oakley pas cher, mulberry, sac guess, sac hermes, vans shoes, sac louis vuitton, vanessa bruno, nike roshe run, louis vuitton, nike roshe, sac longchamp, nike free pas cher, abercrombie and fitch, lululemon, sac burberry, hollister, hollister, louboutin, air force, nike roshe run, louis vuitton uk, north face, nike huarache, ray ban pas cher, hogan outlet, mac cosmetics, new balance pas cher, michael kors pas cher, hollister
ReplyDeletecanada goose, bottega veneta, ghd, hollister, moncler, ugg, canada goose outlet, north face outlet, ugg boots, soccer shoes, canada goose, chi flat iron, uggs outlet, insanity workout, canada goose uk, new balance shoes, iphone 6 cases, north face jackets, babyliss pro, lululemon outlet, air max, beats by dre, marc jacobs, moncler, asics running shoes, moncler outlet, giuseppe zanotti, valentino shoes, jimmy choo outlet, celine handbags, p90x, birkin bag, mcm handbags, reebok outlet, ugg australia, wedding dresses, canada goose, soccer jerseys, ugg boots, rolex watches, mont blanc, nfl jerseys, instyler, ugg pas cher, moncler, timberland boots, herve leger, baseball bats, canada goose jackets, ferragamo shoes
ReplyDeleteافضل شركة رش مبيدات بالدمام
ReplyDeleteشركة مكافحة حشرات بالجبيل
شركة رش حشرات بالطائف
شركة مكافحة حشرات بالقصيم
شركة مكافحة حشرات بخميس مشيط
شركة مكافحة النمل الابيض بالدمام
شركة مكافحة حشرات بالباحة
شركة رش حشرات بالرياض
شركة رش حشرات بالمدينة
ravens jerseys
ReplyDeletecheap ray bans
instyler max
ray ban sunglasses outlet
true religion jeans
converse trainers
michael kors uk
new orleans saints jerseys
cheap jordans
true religion jeans
hermes birkin
ReplyDeletenike shox for men
curry 4 shoes
curry 7
adidas zx flux
golden goose outlet
yeezy boost 350 v2
vans outlet
jordan 12
balenciaga shoes
ReplyDeletecar car نشترى السیارت المصدومة cars بيع سيارات
get redirected herecheck my reference this contact formimportant site Get More Infotry this site
ReplyDeleteclick to investigate Louis Vuitton fake Bags look here replica bags official site bag replica high quality
ReplyDeletehave a peek at this website see here linked here navigate here read here his response
ReplyDeleteview Dolabuy Bottega Veneta look at this site go to the website view publisher site best replica designer bags
ReplyDeleteMore than a great article, but you can learn about a package of distinguished services in buying and selling used, damaged and old cars in the Kingdom of Saudi Arabia through Selling cars in Saudi Arabia We save you a lot of time and effort in selling your car Car buying company in Saudi Arabia So you can get enough information about our distinguished services in Used car buying and selling company Which undoubtedly gives you many of the most wonderful advantages in the field of buying and selling cars Buying all kinds of cars in Saudi Arabia
ReplyDelete