🔒 Locked
rand()%number with float value?
Advertisement
Original Post
take a low integer and divide it by uour number based on hoe precise you want your float to be for example:
or...
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
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
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
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
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]=-
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]=-
whoops, that''s my bad it should read:
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
(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
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
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.
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.
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.**!!
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.**!!
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
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


so write it like that
speedy = ( (float)(rand()%100) ) / 100;
when this dont work, write
speedy = ( (float)(rand()%100) ) / 100.0f;
hope this helps


Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement