Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Unique Random numbers

Author
Message
Mesterkatten
13
Years of Service
User Offline
Joined: 16th Nov 2011
Location:
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
The Zoq2
15
Years of Service
User Offline
Joined: 4th Nov 2009
Location: Linköping, Sweden
Posted: 16th Nov 2011 16:03
Random (1, 100)

Srry about my english im from sweeden
Mesterkatten
13
Years of Service
User Offline
Joined: 16th Nov 2011
Location:
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
Rich Dersheimer
AGK Developer
15
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 16th Nov 2011 17:00
This will give you four unique numbers from 1 to 100.

Mesterkatten
13
Years of Service
User Offline
Joined: 16th Nov 2011
Location:
Posted: 16th Nov 2011 17:19
Awsome, Thanks.
Will try to understand how it works though
Rich Dersheimer
AGK Developer
15
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
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.
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
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()

Rich Dersheimer
AGK Developer
15
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 16th Nov 2011 23:14 Edited at: 16th Nov 2011 23:14
From the help...

Quote: "

SetRandomSeed
Description
Sets 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.
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
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.

-- Jim
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 17th Nov 2011 00: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.


The order is quite important, but it doesn't require any loops so is very efficient.

[b]
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 17th Nov 2011 00:35
Quote: "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...

Rich Dersheimer
AGK Developer
15
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 17th Nov 2011 09:10 Edited at: 17th Nov 2011 09:10
Quote: "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.
Impetus73
13
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 17th Nov 2011 20:43 Edited at: 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.

----------------
AGK user - novice
Did Amiga / AMOS programming in the 90's, just started programming again with AGK.
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 17th Nov 2011 22:10
Quote: "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"


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.

Quote: "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.

Quote: "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.

JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
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.

-- Jim
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 17th Nov 2011 23:44
Quote: "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.

Impetus73
13
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 18th Nov 2011 08:45
Generate a seed and put it in a file, next time the game is started' it reads the last seed from that file.

----------------
AGK user - novice
Did Amiga / AMOS programming in the 90's, just started programming again with AGK.
Marl
13
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
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)



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

Login to post a reply

Server time is: 2024-11-22 16:10:33
Your offset time is: 2024-11-22 16:10:33