Random Function

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

Random Function

Post by RandomDever »

Is there a way to have a random range function that never repeats any digit? :|
User avatar
dandymcgee
ES Beta Backer
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

Post by dandymcgee »

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! :twisted:
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Random Function

Post by MarauderIIC »

Any digit? You mean like
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.
User avatar
dandymcgee
ES Beta Backer
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

Post by dandymcgee »

MarauderIIC wrote:Any digit? You mean like
12345
54321 is ok but
10000 is not?
Wouldn't have thought of it that way.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
RandomDever
Chaos Rift Regular
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

Post by RandomDever »

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
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: Random Function

Post by Bakkon »

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().
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Random Function

Post by MarauderIIC »

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().
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.
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.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Random Function

Post by avansc »

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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
dandymcgee
ES Beta Backer
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

Post by dandymcgee »

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.
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?
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
zodiac976
Chaos Rift Regular
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

Post by zodiac976 »

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.
Last edited by zodiac976 on Thu Jun 25, 2009 9:02 pm, edited 1 time in total.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Random Function

Post by avansc »

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.
do you not read?
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
zodiac976
Chaos Rift Regular
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

Post by zodiac976 »

avansc wrote:
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.
do you not read?
What are you talking about?
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Random Function

Post by avansc »

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"
User avatar
zodiac976
Chaos Rift Regular
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

Post by zodiac976 »

avansc wrote:you basically just dumped the same thing that was already posted by someone else.
srand(((unsigned int)(time(NULL)));
//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.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Random Function

Post by avansc »

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"
Post Reply