Challenge: Generate a Random Float
Forum » Hidden / Challenges » Challenge: Generate a Random Float
started by: hartnellhartnell
on: 1245686506|%e %b %Y, %H:%M %Z|agohover
number of posts: 11
rss icon RSS: new posts
summary:
...
Generate a Random Float
hartnellhartnell 1245686506|%e %b %Y, %H:%M %Z|agohover

The challenge is this :

  • Generate a random floating point number using only the default C++ libraries.

— hartnell

unfold Generate a Random Float by hartnellhartnell, 1245686506|%e %b %Y, %H:%M %Z|agohover
Re: Challenge: Generate a Random Float
kake_fiskkake_fisk 1245693765|%e %b %Y, %H:%M %Z|agohover

This wasn't as easy as just switching to the float data type.
But here's what my results:

#include <iostream>
 
using namespace std;
 
int main()
{
    srand((unsigned)time(0));
    float random_float = (float)rand()/100.0;
    cout << random_float << endl;
    return 0;
}
unfold Re: Challenge: Generate a Random Float by kake_fiskkake_fisk, 1245693765|%e %b %Y, %H:%M %Z|agohover
Re: Challenge: Generate a Random Float
hartnellhartnell 1245698878|%e %b %Y, %H:%M %Z|agohover

This wasn't as easy as just switching to the float data type.

That's what I tried first too. :)

float random_float = (float)rand()/100.0;

Is the (float) in front of the rand() function some type of type casting?

— hartnell

unfold Re: Challenge: Generate a Random Float by hartnellhartnell, 1245698878|%e %b %Y, %H:%M %Z|agohover
Re: Challenge: Generate a Random Float
kake_fiskkake_fisk 1245743480|%e %b %Y, %H:%M %Z|agohover

Yeh, I think that's what it's called.
It converts the value of rand() to float. I don't even think that it's needed when I think about it.
I might be wrong, so anyone feel free to correct me. :)

unfold Re: Challenge: Generate a Random Float by kake_fiskkake_fisk, 1245743480|%e %b %Y, %H:%M %Z|agohover
Re: Challenge: Generate a Random Float
hartnellhartnell 1245765534|%e %b %Y, %H:%M %Z|agohover

I couldn't get it to work. The random number generated doesn't seem to be seeded properly and therefore the number increases as the system clock counts. Arg. I don't know how to solve this.

— hartnell

unfold Re: Challenge: Generate a Random Float by hartnellhartnell, 1245765534|%e %b %Y, %H:%M %Z|agohover
Re: Challenge: Generate a Random Float
u9u9 1245774259|%e %b %Y, %H:%M %Z|agohover
float Random()
{
    static bool seed = true;
    if (seed)
    {
        srand((unsigned)time(0));
        seed = false;
    }
    return (float)rand() / (RAND_MAX + 1);
}

Although, for speed, the user should manually seed the thing… i haven't actually tested this but i use this in my iPhone game, so i certainly hope it works ;) It should return a value in the range [0,1) (or however you write that 1 is not included in the range)… I will make a test of this some day.

(float) casts a value to a floating point number yes, otherwise you will be stuck with integers. If (float) does not have higher precedence then / then this thing will only return 0 as RAND_MAX+1 is larger then any value returned by rand(), and an integer division rounds down.

For the curious of you out there, the static bool seed = true line is only executed the first time the function is called.

unfold Re: Challenge: Generate a Random Float by u9u9, 1245774259|%e %b %Y, %H:%M %Z|agohover
Re: Challenge: Generate a Random Float
kake_fiskkake_fisk 1245778531|%e %b %Y, %H:%M %Z|agohover

Ohh, the static keyword makes the variable just being declared once.
That might be useful if in Game Programming if you execute the function in the game loop.

But it didn't work when I tested it. It returned 0.

#include <iostream>
 
#define RAND_MAX 100
 
using namespace std;
 
float Random()
{
    static bool seed = true;
    if (seed)
    {
        srand((unsigned)time(0));
        seed = false;
    }
    return (float)rand() / (RAND_MAX + 1);
}
 
int main()
{
    float random_number;
    cout << random_number << endl;
    return 0;
}
unfold Re: Challenge: Generate a Random Float by kake_fiskkake_fisk, 1245778531|%e %b %Y, %H:%M %Z|agohover
Re: Challenge: Generate a Random Float
u9u9 1245808601|%e %b %Y, %H:%M %Z|agohover

RAND_MAX is defined in the C++ libraries. You should not redefine it.

unfold Re: Challenge: Generate a Random Float by u9u9, 1245808601|%e %b %Y, %H:%M %Z|agohover
Re: Challenge: Generate a Random Float
kake_fiskkake_fisk 1245818284|%e %b %Y, %H:%M %Z|agohover

Lol, I couldn't know. :p
Anyways, it still returns 0 in my program when I removed the define.

unfold Re: Challenge: Generate a Random Float by kake_fiskkake_fisk, 1245818284|%e %b %Y, %H:%M %Z|agohover
Re: Challenge: Generate a Random Float
u9u9 1245848841|%e %b %Y, %H:%M %Z|agohover

I am not sure what you are doing wrong, so here is an example that gives me random numbers between 0 and 1

#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;

float Random()
{
    static bool seed = true;
    if ( seed )
    {
        srand(time(NULL));
        seed = false;
    }
    return (float)rand() / RAND_MAX;
}

int main( int numArgs, char** args )
{
    for ( int i = 0; i < 5; ++i )
    {
            cout << Random() << endl;
    }
};
unfold Re: Challenge: Generate a Random Float by u9u9, 1245848841|%e %b %Y, %H:%M %Z|agohover
Re: Challenge: Generate a Random Float
kake_fiskkake_fisk 1245853347|%e %b %Y, %H:%M %Z|agohover

Probably forgot ctime. Works now. ^^
How could you generate a higher number then? For example between 0 and 10?

unfold Re: Challenge: Generate a Random Float by kake_fiskkake_fisk, 1245853347|%e %b %Y, %H:%M %Z|agohover
new post