Posted: 16th Nov 2011 15:28
Hello, Im pretty new to AppGameKit and programming in general and I just stumbled upon my first problem.

I need to generate say 4 unique numbers between 1-100. I have tried alot of different ideas but nothing seems to work. Anyone has any ideas how this can be done ? Thanks
Posted: 16th Nov 2011 16:03
Random (1, 100)
Posted: 16th Nov 2011 16:09
but I will not get unique numbers if im doing more then one right?

this:

rnd1 = random (1, 100)
rnd2 = random (1, 100)
rnd3 = random (1, 100)
rnd4 = random (1, 100)

might result in 5,5,5,5
Posted: 16th Nov 2011 17:00
This will give you four unique numbers from 1 to 100.

+ Code Snippet
dim number[100]
for x = 1 to 100
    number[x] = x
next x

do
 Print("Click or tap to get 4 unique random numbers")
    for x = 1 to 4
        print(number[x])
    next x
    if GetPointerReleased()
        for x = 1 to 1000
            n1 = Random(1,4)
            n2 = random(5,100)
            temp = number[n1]
            number[n1] = number[n2]
            number[n2] = temp
        next x
    endif
 Sync()
loop
Posted: 16th Nov 2011 17:19
Awsome, Thanks.
Will try to understand how it works though
Posted: 16th Nov 2011 19:20
It's like shuffling a deck of cards. Each card is always unique, but it changes position in the deck. The numbers are randomly swapped in the array 1000 times, you then use the first four.
Posted: 16th Nov 2011 22:43
I would use exactly the same method for unique numbers. But I get paranoid and shuffle 100 times more than the numbers in the array (e.g 100 numbers = 10000 swaps)

Does AppGameKit have a problem with randomising the random numbers? For example, most languages have the same series of random numbers, in DBP we RANDOMIZE TIMER()
Posted: 16th Nov 2011 23:14
From the help...

ets the seed for the random number generator. Two AppGameKit applications using the same seed value will generate the same sequence of random numbers. By default the seed is set to the current time on startup so that each run of the application will generate a different sequence of numbers.


DBPro does a silent randomize at the start of program execution as well, I believe.
Posted: 16th Nov 2011 23:21
We could argue about what is actually random for years.

A shuffled pack is pseudo-random, which is probably fine for most game purposes. All computers use algorithms to produce a random series.

Spark-gaps are the best thing. Get your users to stick a mains cable across the keyboard. Bit of a problem on phones or pads which don't have a keyboard.

For truly random numbers we would need to sample an analogue source - maybe a microphone - and use that with suitable range conversion.
Posted: 17th Nov 2011 0:21
One way is to look at it like this: to get 4 random numbers in the range 1-100 you are first getting 1 number from 100 options, then 1 number from 99 options, then 1 from 98, and finally 1 from 97.

ie.
+ Code Snippet
a = rnd(99)+1
b = rnd(98)+1
c = rnd(97)+1
d = rnd(96)+1
if b >= a then inc b
if c >= a then inc c
if d >= a then inc d
if c >= b then inc c
if d >= b then inc d
if d >= c then inc d


The order is quite important, but it doesn't require any loops so is very efficient.
Posted: 17th Nov 2011 0:35
Does AppGameKit have a problem with randomising the random numbers? For example, most languages have the same series of random numbers, in DBP we RANDOMIZE TIMER()

I read a discussion on this between Green Gandalf and IanM a while ago and it seems you are always best to NOT use "setRandomSeed" or "randomise" unless you want repetative results. If you set a seed no matter what the source it makes it repetative.

The best way is to just call the "random" function and ignore seeds unless you want the same results each time your game is run.

I tested this in AppGameKit and when I used "setRandomSeed(timer())" I got repetative results almost every time...
Posted: 17th Nov 2011 9:10
For truly random numbers we would need to sample an analogue source - maybe a microphone - and use that with suitable range conversion


Yes, I read somewhere that NSA/CIA once used encryption based on random numbers generated by atmospheric noise.
Posted: 17th Nov 2011 20:43
Pick a random number, compare it with the last one, if the same, pick it again, until you have 4 unique ones.
Posted: 17th Nov 2011 22:10
f d >= c then inc d


This one doesn't seem right to me. Firstly, you are more likely to pick number 1 than number 100 - 100 is only in one of the four selections whereas 1 is in all of them.
Secondly you have more chance of getting (for example) 51 than any other number if 50 has already been chosen, it looks like you are 4 times more likely to get a+1 than any other number, 3 times more likely to get b+1 and twice as likely to get c+1.

I tested this in AppGameKit and when I used "setRandomSeed(timer())" I got repetative results almost every time...


In AppGameKit, Timer() is the time since the start of the game, so it's extremely likely you'll get repetitive results if you randomise as you start, and reasonably likely if you randomise when the user plays the first game (if they press start as soon as its available).
In DBP, Timer() is based on the system clock, so it's much harder if not virtually impossible to get the same seed. Additionally, if you randomise more than once in your game, it becomes more unpredictable.

I read a discussion on this between Green Gandalf and IanM a while ago and it seems you are always best to NOT use "setRandomSeed" or "randomise" unless you want repetative results. If you set a seed no matter what the source it makes it repetative.

I can't get my head around this one. As mentioned above if you want to break the preset sequences, then surely you need to randomise at numerous intervals, otherwise your program reverts to the established patterns over time.
Posted: 17th Nov 2011 23:10
There is a difference between a sequence, like a shuffled deck, and a random number. A random number may come up any number of times in succession.

A lot depends on what you want the "random" number for. Unless you can provide an unpredictable seed you're going to get the same sequence out of the random() function.

Waiting for a user key press is okay, so long as you can extract the fractional part of the time-stamp. Obvious seconds are no good, but milliseconds may be.
Posted: 17th Nov 2011 23:44
There is a difference between a sequence, like a shuffled deck, and a random number. A random number may come up any number of times in succession.


The original request is for 4 unique random numbers, hence the need to take a shuffled sequence. You could also keep generating numbers until you had 4 unique ones in a fashion like Diggsey's example.
Posted: 18th Nov 2011 8:45
Generate a seed and put it in a file, next time the game is started' it reads the last seed from that file.
Posted: 19th Nov 2011 17:28
code to create any number (can be 4) of unique random numbers within a defined range (can be 1 to 100)

+ Code Snippet
// Generate Unique Random Numbers

//	Set Quantity and range of numbers wanted

wantedNumbers = 4
loNumber = 1
HiNumber = 100
dim result [ wantedNumbers - 1 ]

//	Generate numbers and check for duplicates

thisNumber = 0
repeat
	result [ thisNumber ] = random ( loNumber , HiNumber )
	for checkNumber = 0 to thisNumber-1
		if result [ checkNumber ] = result [ thisNumber ] then result [ thisNumber ] = loNumber - 1
	next checkNumber
	if result [ thisNumber ] > loNumber - 1 then thisNumber = thisNumber + 1
until thisNumber = wantedNumbers

// Basic Output Routine to complete the App

do
	for count = 0 to wantedNumbers - 1
		printc ( right ( spaces ( 2 ) + str ( count + 1 ) , 3) )
		printc ( " : " )
		print ( right ( spaces ( 2 ) + str ( result [ count ] ) , 3 ) )
	next count
	sync ()
loop


Probably best not to get it to return 101 unique numbers from a 100 number range though