Register | Login | |||||
Main
| Memberlist
| Active users
| Calendar
| Chat
| Online users Ranks | FAQ | ACS | Stats | Color Chart | Search | Photo album |
| |
0 users currently in Programming. |
User | Post |
HyperHacker Posts: 284/5072 |
Did you read the thread? rand() already returns a whole number. |
Sukasa Posts: 114/2068 |
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. |
MisterJones Posts: 5/125 |
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() |
Omega45889 Posts: 18/92 |
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/ |
Zem Posts: 135/1097 |
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... |
Disch Posts: 2/202 |
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). |
MisterJones Posts: 3/125 |
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) |
Randy53215 Posts: 29/726 |
Im not familiar with many other languages but rand() in PHP would be something as short as:
Hope that help's. |
MissingName Posts: 15/18 |
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! |
neotransotaku Posts: 118/1860 |
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 Posts: 14/18 |
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. |