
Random Function
Moderator: Coders of Rage
-
- Chaos Rift Regular
- Posts: 198
- Joined: Thu Mar 26, 2009 8:42 pm
- Current Project: My Engine
- Programming Language of Choice: C++
Random Function
Is there a way to have a random range function that never repeats any digit? 

My first game: http://donsvoice.com/randomdever/Duck%2 ... 01.0.0.zip
My second game: http://donsvoice.com/randomdever/Space%20Invaders.zip
My third game: http://donsvoice.com/randomdever/BreakOut!.zip
My second game: http://donsvoice.com/randomdever/Space%20Invaders.zip
My third game: http://donsvoice.com/randomdever/BreakOut!.zip
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: Random Function
Use a normal random number generator, but store each generated number somewhere and check against it. If you find a match generate a new number. Dunno if there's a better way.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: Random Function
Any digit? You mean like
12345
54321 is ok but
10000 is not?
12345
54321 is ok but
10000 is not?
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: Random Function
Wouldn't have thought of it that way.MarauderIIC wrote:Any digit? You mean like
12345
54321 is ok but
10000 is not?
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
-
- Chaos Rift Regular
- Posts: 198
- Joined: Thu Mar 26, 2009 8:42 pm
- Current Project: My Engine
- Programming Language of Choice: C++
Re: Random Function
No I mean like if i ran the program:
1
2
3
4
5
6
3
6
(Which is okay
)
Then I run it again:
1
2
3
4
5
6
3
6
That's repeating
1
2
3
4
5
6
3
6
(Which is okay

Then I run it again:
1
2
3
4
5
6
3
6
That's repeating
My first game: http://donsvoice.com/randomdever/Duck%2 ... 01.0.0.zip
My second game: http://donsvoice.com/randomdever/Space%20Invaders.zip
My third game: http://donsvoice.com/randomdever/BreakOut!.zip
My second game: http://donsvoice.com/randomdever/Space%20Invaders.zip
My third game: http://donsvoice.com/randomdever/BreakOut!.zip
- Bakkon
- Chaos Rift Junior
- Posts: 384
- Joined: Wed May 20, 2009 2:38 pm
- Programming Language of Choice: C++
- Location: Indiana
Re: Random Function
That's because the rand() function picks a number based on how long the program's been running or some such. You need to generate a new seed before using random numbers. Use srand(#) where you feed it in something like timeGetTime() or SDL_GetTicks().
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: Random Function
I usually feed it time() (from #include <ctime>) on the first call to srand and every subsequent seed I use the last result from rand(). I find this tends to give me better non-repetitiveness than just seeding with time() every time. YMMV.Bakkon wrote:That's because the rand() function picks a number based on how long the program's been running or some such. You need to generate a new seed before using random numbers. Use srand(#) where you feed it in something like timeGetTime() or SDL_GetTicks().
Like this:
Code: Select all
#include <ctime> //time
#include <iostream> //cout
#include <cstdlib> //rand
using namespace std;
int main() {
srand(time(NULL));
unsigned int num = rand();
cout << "Something between 0 and 9: " << num % 10 << endl;
srand(num);
return 0;
}
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Re: Random Function
what mar suggested would work and is how i learnt as well. but im not sure what randomization algorithm C uses.
if its anything like a montecarlo based algo its not completely random and will repeat. even to the point where you can predict the numbers.
but in all reality you wont notice it.
if its anything like a montecarlo based algo its not completely random and will repeat. even to the point where you can predict the numbers.
but in all reality you wont notice it.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: Random Function
The most important factor in calculating random sequences is always what you plan to use it for. Top Secret level encryption, or a number guessing game?avansc wrote:what mar suggested would work and is how i learnt as well. but im not sure what randomization algorithm C uses.
if its anything like a montecarlo based algo its not completely random and will repeat. even to the point where you can predict the numbers.
but in all reality you wont notice it.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- zodiac976
- Chaos Rift Regular
- Posts: 156
- Joined: Thu Jun 18, 2009 10:03 am
- Current Project: Booklet & Text RPG
- Favorite Gaming Platforms: PC, PS3, PSP
- Programming Language of Choice: C++
- Location: AL
- Contact:
Re: Random Function
If you don't want the same random numbers to appear over and
over you need to set a seed first using srand() for instance. If
that is what you're asking.
Use a seed like this... srand(((unsigned int)(time(NULL)));
Also use the <ctime> and <cstdlib> includes.
over you need to set a seed first using srand() for instance. If
that is what you're asking.
Use a seed like this... srand(((unsigned int)(time(NULL)));
Also use the <ctime> and <cstdlib> includes.
Last edited by zodiac976 on Thu Jun 25, 2009 9:02 pm, edited 1 time in total.
Re: Random Function
do you not read?zodiac976 wrote:If you don't want the same random numbers to appear over and
over you need to set a seed first using srand() for instance. If
that is what you're asking.
Use a seed like this... srand((unsigned int)(time(NULL));
Also use the <ctime> and <cstdlib> includes.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- zodiac976
- Chaos Rift Regular
- Posts: 156
- Joined: Thu Jun 18, 2009 10:03 am
- Current Project: Booklet & Text RPG
- Favorite Gaming Platforms: PC, PS3, PSP
- Programming Language of Choice: C++
- Location: AL
- Contact:
Re: Random Function
What are you talking about?avansc wrote:do you not read?zodiac976 wrote:If you don't want the same random numbers to appear over and
over you need to set a seed first using srand() for instance. If
that is what you're asking.
Use a seed like this... srand((unsigned int)(time(NULL));
Also use the <ctime> and <cstdlib> includes.
Re: Random Function
you basically just dumped the same thing that was already posted by someone else.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- zodiac976
- Chaos Rift Regular
- Posts: 156
- Joined: Thu Jun 18, 2009 10:03 am
- Current Project: Booklet & Text RPG
- Favorite Gaming Platforms: PC, PS3, PSP
- Programming Language of Choice: C++
- Location: AL
- Contact:
Re: Random Function
srand(((unsigned int)(time(NULL)));avansc wrote:you basically just dumped the same thing that was already posted by someone else.
//for integers
int randNum = rand() % 100;
//for precision numbers
randNum = (randNum / 100) + rand() % 100;
Almost the same but not exactly
Last edited by zodiac976 on Thu Jun 25, 2009 9:03 pm, edited 1 time in total.
Re: Random Function
thats what you call spin. you should be in politics.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"