You are not logged in.
After checking Shane's code for the framework "GameplayKit"
I made Apple's example in Objective-C to ASOC.
Applescript:
use framework "Foundation"
use framework "GameplayKit"
use scripting additions
(** Objective-C
* Reference: https://developer.apple.com/documentation/gameplaykit/gkgaussiandistribution?language=objc
* GKRandomSource *random = [[GKRandomSource alloc] init];
* GKRandomDistribution *dice3d6;
* dice3d6 = [[GKGaussianDistribution alloc]
* initWithRandomSource:random lowestValue:3 highestValue:18];
* // Roll the dice...
* int diceRoll = [dice3d6 nextInt];
*)
set randomSource to current application's GKRandomSource's alloc()'s init()
set dice3d6 to current application's GKGaussianDistribution's alloc()'s initWithRandomSource:randomSource lowestValue:3 highestValue:18
return (dice3d6's nextInt()) as integer
Applescript:
set randomSource to current application's GKRandomSource's alloc()'s init()
set shuffled to current application's GKShuffledDistribution's alloc()'s initWithRandomSource:randomSource lowestValue:1 highestValue:6
-- return (shuffled's nextInt()) as integer
-- Log the output of 100
set shuffleDist to current application's GKShuffledDistribution's d6
repeat with i from 1 to 100
log {shuffleDist's nextInt() as integer}
end repeat
Applescript:
-- Reference: https://developer.apple.com/documentation/gameplaykit/gkrandomsource/1501128-arraybyshufflingobjectsinarray
-- Returns an array whose contents are the same as those of the specified array, but in a random order determined by the random source.
set theArray to {1,2, 3, 4, 5, 6, 7, 8, 9}
set theArray to current application's NSArray's arrayWithArray:theArray
set randomSource to current application's GKRandomSource's alloc()'s init()
set shuffled to randomSource's arrayByShufflingObjectsInArray:theArray
return shuffled as list
Last edited by Fredrik71 (2020-10-06 03:08:53 pm)
The purpose to study someone else art is not to add, its to make less more.
Offline
Fredrik,
is there a reason you are using text instead of integers in the line:
Applescript:
initWithRandomSource:randomSource lowestValue:"3" highestValue:"18"
upon reading the documentation for GameplayKit, it seems to be expecting numbers like so
Applescript:
initWithRandomSource:randomSource lowestValue:3 highestValue:18
Just wondering
Offline
No reason, you are right I will change it...
The purpose to study someone else art is not to add, its to make less more.
Offline
I'm currently using
Applescript:
set randomSource to current application's GKRandomSource's alloc()'s init()
set randItem to current application's GKRandomDistribution's alloc()'s initWithRandomSource:randomSource lowestValue:1 highestValue:c
(randItem's nextInt()) as integer
I would like to use
func nextInt(upperBound: Int) -> Int
I dont know how to write it correctly in AppleScriptObjC
** EDIT
I figured it out below
Applescript:
(randItem's nextIntWithUpperBound:i) as integer
Last edited by robertfern (2020-10-07 01:26:29 pm)
Offline