Register | Login | |||||
Main
| Memberlist
| Active users
| Calendar
| Chat
| Online users Ranks | FAQ | ACS | Stats | Color Chart | Search | Photo album |
| |
0 users currently in Programming. |
Acmlm's Board - I3 Archive - Programming - Basic and how to use rand() correctly | New poll | | |
Add to favorites | Next newer thread | Next older thread |
User | Post | ||
MissingName Micro-Goomba Since: 11-22-05 From: Fort Worth Last post: 6733 days Last view: 6733 days |
| ||
I'm programming in a Basic-like programming languages. All I know is that the only way I can create a random integer is using rand().
However, I don't know how to use this function correctly. May someone show me the light? Thanks. (edited by MissingName on 12-06-05 11:35 PM) |
|||
neotransotaku Sledge Brother Liberated from school...until MLK day Since: 11-17-05 From: In Hearst Field Annex... Last post: 6299 days Last view: 6297 days |
| ||
I believe rand() returns a number between 0 and 1. So, if you want an integer you need to scale the number accordingly by multiplying it.
Is this a graphic calculator? |
|||
MissingName Micro-Goomba Since: 11-22-05 From: Fort Worth Last post: 6733 days Last view: 6733 days |
| ||
Nope, it's not a graphing calculator. At least a graphing calculator has randint(), which I do know how to use.
But if rand() does produces a small number, that's good. I believe it's time for expirementation. Huzzah! |
|||
Randy53215 Melon Bug Since: 11-17-05 From: Greenfield, Wisconsin (U.S.A) Last post: 6298 days Last view: 6297 days |
| ||
Im not familiar with many other languages but rand() in PHP would be something as short as:
Hope that help's. (edited by Randy53215 on 12-09-05 02:16 PM) |
|||
MisterJones Tooky Since: 12-08-05 From: Mexico Last post: 6387 days Last view: 6323 days |
| ||
PHP treats the random function a little to different than in many other languages, as it sets a range already. It's more common to see it returning a value between 0 and 1 (although on C it's kind of weird IIRC) | |||
Disch Red Cheep-cheep Since: 12-10-05 Last post: 6577 days Last view: 6577 days |
| ||
Originally posted by MisterJones C is just typically more geared towards integers, so rand() gives a random integer value (between 0 and RAND_MAX, which I believe to be defined as 0x7FFF). However that's easily transformed to a usable range with a simple mod operation: int something = rand() % 10; That will give you a random number between 0-9 (inclusive). |
|||
Zem Permabanned. Flaming, trolling, reregistering. Since: 11-18-05 Last post: 6620 days Last view: 6620 days |
| ||
Originally posted by DischOriginally posted by MisterJones ? That's not the best way to do it, it wouldn't be properly weighted. I'll show how with a small-scale example: Let's say RAND_MAX was 8, and you wanted a number between 0 and 4, so you did rand() % 5. This has (presumably) an equal probability of coming up with each of the following: 0 % 5 = 0 1 % 5 = 1 2 % 5 = 2 3 % 5 = 3 4 % 5 = 4 5 % 5 = 0 6 % 5 = 1 7 % 5 = 2 8 % 5 = 3 You'd end up with a 2/9 chance to get any of 0, 1, 2, or 3, and a 1/9 chance to get 4. Obviously this effect diminishes as the distance between RAND_MAX and your mod increases, but there must be a better way to scale it... |
|||
Omega45889 Shyguy Since: 11-18-05 Last post: 6330 days Last view: 6345 days |
| ||
Well, this is how it works in C/C++.
You must first seed rand with a random number like say, tickcount. so, the code would look somethin like this srand(GetTickCount()); // seed rand() with system tickcount int randomNumber = rand(); // call rand, store return value in randomNumber and then to set a max value for the random number you generated, you could just do somethin simple like: randomNumber %= 400; // garantees that the randomNumber is between 0 and 400 or, you could use somethin like rand_s which is just rand() with security features. Probably not required in your case, and overly complicated. Now, i dont know much VB, so its probably a little different, but remember, MSDN is your friend. http://msdn.microsoft.com/ |
|||
MisterJones Tooky Since: 12-08-05 From: Mexico Last post: 6387 days Last view: 6323 days |
| ||
Originally posted by DischOriginally posted by MisterJones Yeah, I was aware of that, I just meant that behave it much more different than many other randomizers. I didn't remember that it only returned integers tho. Been a while. Originally posted by Zem You could try the non-ANSI random() |
|||
Sukasa Birdo Not quite as active as before. Xkeeper supporter Xk > ||bass I IP Banned myself! Twice! Since: 11-17-05 From: Somewhere over there Last post: 6298 days Last view: 6297 days |
| ||
Try this:
a = INT(rand() * 10) That should help- it multiplies the output of rand() by 10, and then it rounds it off to the nearest whole number. |
|||
HyperHacker Star Mario Finally being paid to code in VB! If only I still enjoyed that. <_< Wii #7182 6487 4198 1828 Since: 11-18-05 From: Canada, w00t! My computer's specs, if anyone gives a damn. STOP TRUNCATING THIS >8^( Last post: 6298 days Last view: 6298 days |
| ||
Did you read the thread? rand() already returns a whole number. |
Add to favorites | Next newer thread | Next older thread |
Acmlm's Board - I3 Archive - Programming - Basic and how to use rand() correctly | | |