Skip to main content
gamedev.net
🔒 Locked

rand()%number with float value?

Started by Dunge Nov 18, 2000 at 2:22 PM 13 replies 2.6k views
Original Post
Dunge
Dunge
I know that rand()%5 will get a number from 0 to 5 but how can it work with float value like 0.05f?
oglman
oglman
take a low integer and divide it by uour number based on hoe precise you want your float to be for example:

1/(rand()%5+1)=[1, 0.5, 1/3, 0.25, 0.2]
2/(rand()%5+1)=[2, 1, 2/3, 0.5, 0.4]

or...

(rand%100)/100=[0, 0.01, 0.02, 0.03...0.97, 0.98, 0.99, 1]
(rand%100)/50=[0, 0.02, 0.04, 0.06...1.94, 1.96, 1.98, 2]

The second example takes an int and moves it''s decimal over two places and in the second insatnce of that example, it then multipiles it by 2.




Open mouth, insert foot
Dunge
Dunge
Humm I don't understand that thing and it don't work

speedy = (rand%100)/100;
that's what you mean? anyway it give me

C:\download\Pongl\PONGL.cpp(85) : error C2296: '%' : illegal, left operand has type 'int (__cdecl *)(void)'

.... I always need to put the f somewhere at the end

Edited by - Dunge on November 18, 2000 3:46:40 PM
Megahertz
Megahertz
rand()%100 gives u a number between 0 and 100

take the result of this an divide it by 100

so if u get 45 out of rand()%100 , divide that by 100

so u get 0.45


and if rand()%100 return 76, and u divide that by 100 u get .76

if u did (rand()%1000)/1000 u would get a number between 0 and 1 with 3 decimal places of precision
.004 or .452 or .762 where as u only got 2 with 100, .02 or .34

i hope u can see the patteren.

-=[Megahertz]=-

-=[Megahertz]=-
oglman
oglman
whoops, that''s my bad it should read:

(rand()%100)/100=[0, 0.01, 0.02, 0.03...0.97, 0.98, 0.99, 1]
(rand()%100)/50=[0, 0.02, 0.04, 0.06...1.94, 1.96, 1.98, 2]

and from that error message it looks like you need to make speedy a float value if you want to stop getting the error




Open mouth, insert foot
skyfire360
skyfire360
quote:
Original post by Dunge
speedy = (rand%100)/100;


Dunge, you should make sure speedy is of type float and this line should read:

speedy = (rand()%100)/100;

you forgot the () after the rand

-SkyFire

I do real things with imaginary numbers
Strylinys
Strylinys
Actually that error means exactly what it says...

Basically: "The number on the left of the ''%'' operator is not an int, it''s an int (__cdecl *)(void). I don''t like that. Change it or I won''t work."

That''s all it means... This happened because ''rand'' is the name of the function, but without putting ''rand()'', it is not executed properly and that means it doesn''t return a value.

S.
Dunge
Dunge
Ok ok I put the () after rand (that''s not my error lol) but I always get
C:\download\Pongl\PONGL.cpp(138) : warning C4244: ''='' : conversion from ''int'' to ''float'', possible loss of data

this is only a warning, I can play but when I have my collision with the ball and it call this function it look like it put speedy to 0,.... the ball don''t move on the Y axis...

I define speedy like that :
GLfloat speedy = 0.03f; //0.03f; is the speed at the begginning

that''s why I said I must get 0.**f and not 0.**!!
skyfire360
skyfire360
quote:
Original post by Dunge
C:\download\Pongl\PONGL.cpp(138) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of data

GLfloat speedy = 0.03f; //0.03f; is the speed at the begginning



Assuming you have both this line in your code, make it say:

speedy = float((rand()%100)/100);



Edited by - skyfire360 on November 18, 2000 4:27:51 PM
I do real things with imaginary numbers
Dunge
Dunge
Humm it give no error or warning but put the value of speedy to 0 again
davepermen
davepermen
you have to convert the result from rand()%100, wich is saved as an int to a float, else you have an integer division of an integervalue / integervalue-100. that gives 1 or 0 as result, nothing else (because its between 0/100 and 100/100)

so write it like that

speedy = ( (float)(rand()%100) ) / 100;

when this dont work, write
speedy = ( (float)(rand()%100) ) / 100.0f;

hope this helps

we wanna play, not watch the pictures

If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
Dunge
Dunge
Cool it''s working!!!!!!! thanks everybody!!!
davepermen
davepermen
no problem, do you have any more problems you need to be helped out, i dont have to do anything currently

we wanna play, not watch the pictures

If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
oglman
oglman
Yesterday I made a random number generator that generates truly random numbers (ie. precise to 0.000001) between 1 and 100 by seeding the random number genator at random intrevals. (Not really random but unpredictable).




Open mouth, insert foot

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.