(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
06-13-24 09:41 AM
0 users currently in Programming.
Acmlm's Board - I3 Archive - Programming - Basic and how to use rand() correctly
  
User name:
Password:
Reply:
 
Options: - -
Quik-Attach:
Preview for more options

Max size 1.00 MB, types: png, gif, jpg, txt, zip, rar, tar, gz, 7z, ace, mp3, ogg, mid, ips, bz2, lzh, psd

UserPost
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 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()
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 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...
Disch
Posts: 2/202
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).
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:


rand(1, 100)

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


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.
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.011 seconds; used 358.03 kB (max 410.31 kB)