|
-
May 18th, 2002, 07:44 PM
#1
Thread Starter
Addicted Member
random number
how would you generate a random number between 1 and 52? including 1 and 52
-
May 18th, 2002, 08:24 PM
#2
PowerPoster
Try
PHP Code:
int mynum = rand()%1+52;
or
PHP Code:
int mynum = rand()% 53;
if it doesn't include 1 and 52;
-
May 23rd, 2002, 01:05 AM
#3
New Member
-
May 23rd, 2002, 10:15 AM
#4
why you use "%" and not "*" ?
In VisualBasic or in Java we never use modulo for random operation ?
-
May 23rd, 2002, 01:15 PM
#5
Monday Morning Lunatic
Code:
int num = int((float(rand()) / RAND_MAX) * 52.0) + 1;
% isn't valid for floating-point numbers...
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 23rd, 2002, 05:18 PM
#6
anyone can tell me what rand() return ? I have done some test and i return about 3 to 6 decimal number ? why isn't between 0 and 1 like VB or Java?
I do not understand why we use MOD (%) ? Anyone can tell me a mathematic answer about that ?
-
May 23rd, 2002, 05:27 PM
#7
Monday Morning Lunatic
You shouldn't need modulus...
rand() returns a number between 0 and RAND_MAX. So, to get it between 0 and 1, divide rand() by RAND_MAX. Then just multiply by 52.
I can't see where % would come into it.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 24th, 2002, 10:27 AM
#8
It's very simple. Most random functions return a flloating point number between 0 and 1. CRT rand() returns an integer between 0 and RAND_MAX (which is defined as 32767, the highest positive integer possible for a 16 bit signed variable). To get a number between 0 and n (exclusive), simply do
rand() % n;
which puts all numbers in this area. It might not be completly fair (the lowest numbers have a slightly higher probability), but it is fast.
A slightly more fair approach is parksie's snippet, but it is considerably slower, having one FDIV, one FMUL, two int-to-float conversions (one on good compilers), one ADD and one float-to-int conversion as opposed to on DIV of the first approach (386 DIV instruction computes division and modulo at the same time).
So, if you don't need any special accuracy, use %. Else, use the floating thing or wait for quantum computers, which can produce real random numbers.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
May 24th, 2002, 11:28 AM
#9
Monday Morning Lunatic
Originally posted by CornedBee
So, if you don't need any special accuracy, use %. Else, use the floating thing or wait for quantum computers, which can produce real random numbers.
Or give a number to your users and tell them to multiply it by two and enter it in again. Chances are, none of them will ever give the same number
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 27th, 2002, 07:40 AM
#10

Unless you're stuck with people who mindlessly follow any instructions the program gives them.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|