(Link to AcmlmWiki) Offline: thank ||bass
Register | Login
Views: 13,040,846
Main | Memberlist | Active users | Calendar | Chat | Online users
Ranks | FAQ | ACS | Stats | Color Chart | Search | Photo album
05-17-24 05:21 AM
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
Posted on 12-07-05 12:22 AM Link | Quote
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
Posted on 12-07-05 02:13 PM Link | Quote
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
Posted on 12-07-05 02:31 PM Link | Quote
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
Skype
Posted on 12-09-05 03:15 PM Link | Quote
Im not familiar with many other languages but rand() in PHP would be something as short as:


rand(1, 100)

## -- Would select a number between 1 and a 100 -- ##


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
Posted on 12-09-05 06:12 PM Link | Quote
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
Posted on 12-10-05 02:44 PM Link | Quote
Originally posted by MisterJones
(although on C it's kind of weird IIRC)


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
Posted on 12-12-05 02:16 AM Link | Quote
Originally posted by Disch
Originally posted by MisterJones
(although on C it's kind of weird IIRC)


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).

?

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
Posted on 12-12-05 07:14 AM Link | Quote
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
Posted on 12-19-05 12:03 PM Link | Quote
Originally posted by Disch
Originally posted by MisterJones
(although on C it's kind of weird IIRC)


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).


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
?

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...


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
Posted on 12-19-05 03:17 PM Link | Quote
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
Posted on 12-23-05 11:10 AM Link | Quote
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 |


ABII

Acmlmboard 1.92.999, 9/17/2006
©2000-2006 Acmlm, Emuz, Blades, Xkeeper

Page rendered in 0.014 seconds; used 400.77 kB (max 501.07 kB)