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 / How to make a random number generator that doesent repeat numbers?

Author
Message
Brny
9
Years of Service
User Offline
Joined: 12th Oct 2015
Location:
Posted: 2nd Dec 2015 20:40
I'm making a program and i'm going crazy with that... i hope someone can help me...

I tried to do it as the code i attach, but it doesent works and i really dont get how to do it...

Timshark
17
Years of Service
User Offline
Joined: 30th Jun 2007
Location: Oslo, Norway
Posted: 2nd Dec 2015 22:17 Edited at: 2nd Dec 2015 22:18
One way to do it is to use 2 arrays. One array for the set of numbers you want to pick from, and another array where you put the random picks.
When you pick a number, you delete it from the first set of numbers.

Like this:

Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 2nd Dec 2015 22:20 Edited at: 2nd Dec 2015 22:27
u can also change the content.
u said u want only 1-5 number once in this array.

example:
AGK (Steam) V2.0.15d : Windows 10 Pro 64 Bit : AMD (15.201.1151.0) Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 3rd Dec 2015 09:21
I use Markus' method to shuffle a deck of cards.
The advantage is that you can shuffle the numbers again at any time very easily. Create a function to do the shuffling and pass in any array.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Timshark
17
Years of Service
User Offline
Joined: 30th Jun 2007
Location: Oslo, Norway
Posted: 3rd Dec 2015 15:18 Edited at: 3rd Dec 2015 17:34
I agree with BatVink.

Markus, nondestructive shuffling is the better way.

This is Markus version - as a function you can add to your code:




Tim
Brny
9
Years of Service
User Offline
Joined: 12th Oct 2015
Location:
Posted: 4th Dec 2015 05:12
Hi guys, thanks for the examples i'm studying them to see which one is the best for my purpose, i'll need to manage a range of numbers like 1 to 50, also i need something that counts how many number have

appeared and when all the numbers are "picked" and theres no unused numbers left of the range, it steps to the next phase (a scoreboard or whatever).

So, i think the Timshark code is just what i need, i think i just need to add something like "if numberset[x]= 0 blablabla", im going to test that code now when i have a little free time to see how to do that...




Brny
9
Years of Service
User Offline
Joined: 12th Oct 2015
Location:
Posted: 8th Dec 2015 00:31
Hi, i made some tests, the code of Markus, it shows a bunch of ¿5? numbers and one is repeated near everytime.

I made a variation of it, that every more or less 4 times/compilations/executions or more it repeats one number, but, ¿why? i donnow, it's a mistery of the universe.

So i just started making a different program, till i have enough motivation to continue playing with the crazy code of that one just to not repeat the numbers....
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 8th Dec 2015 17:39
often this random numbers are based on a start seed.
as example, minecraft show you a seed number and other people can use the same start seed and get the same "random" world.
just saw, we have two random generators in agkv2.
for "real" random the start seed should vary by clock/time value each app start.

Random ( )
Random2 ( )
SetRandomSeed ( seed )
SetRandomSeed2 ( seed )
AGK (Steam) V2.0.15d : Windows 10 Pro 64 Bit : AMD (15.201.1151.0) Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)
Brny
9
Years of Service
User Offline
Joined: 12th Oct 2015
Location:
Posted: 25th Jan 2016 08:11 Edited at: 25th Jan 2016 08:13
Thanks Marcus, that was a very useful information

I had noticed that the random was not really really random.... now i understand it
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 25th Jan 2016 08:49
Snippet which gets a random card from a deck until the deck is empty then starts the deck over


"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Brny
9
Years of Service
User Offline
Joined: 12th Oct 2015
Location:
Posted: 25th Jan 2016 11:15
Ah ok, so with the DEC function you decrease the deck size by 1 every step until is empty... cool.

So, now i must just make a comparator between the given number and the used numbers that will be stored in one array.... cool, i am happy to start seeing the road again.

Thanks.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 25th Jan 2016 20:06
Yes, and whatever random index you pulled gets swapped with the last readable item in the array. So it does sort of shuffle the deck constantly. The actual array size never changes, only the range from which we pick our number.

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

Login to post a reply

Server time is: 2024-11-17 03:05:40
Your offset time is: 2024-11-17 03:05:40