473,598 Members | 3,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rand() with base

Hi,
I want to generate a random 8 bit number using rand(0
is that possible? to expecifu the base and the lenght?
thanks

Nov 14 '05 #1
36 2670
Profetas <xu*****@yahoo. com> scribbled the following:
Hi,
I want to generate a random 8 bit number using rand(0
is that possible? to expecifu the base and the lenght?
thanks


"Base" and "length" do not apply to numbers, only representations of
them. Therefore you generate an 8 bit number the same way you generate
any other number.
Now an 8 bit number has values from 0 to 255. Therefore you only need to
"cut down" the number returned by rand() in either of two ways: you can
modulo it by 256 (%256) or bitwise-AND it with 255 (&255).

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Outside of a dog, a book is a man's best friend. Inside a dog, it's too dark
to read anyway."
- Groucho Marx
Nov 14 '05 #2
I need something like 110101101010101 0.
to be generated randomly and automatically.
How do I specify the rand() limit? for example I want a random number of
10?
this way I can say I want a random number of 01.
Nov 14 '05 #3
Profetas <xu*****@yahoo. com> scribbled the following:
I need something like 110101101010101 0.
That's not an 8 bit number. Anyway, you need to understand that the
difference between bases only comes into play when you are printing
numbers out, or reading them in. Internally, a number is a number is a
number.
to be generated randomly and automatically.
How do I specify the rand() limit? for example I want a random number of
10?
I just told you. The rand() function returns a value from 0 to RAND_MAX.
Use the % or & operators to keep it within the range you want.
When you say "a random number of 10", do you mean ten, or 10 in binary,
i.e. two? If it's the former, then you can use rand()%10. If it's the
latter, you can use rand()%2 or rand()&1.
this way I can say I want a random number of 01.


--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"'So called' means: 'There is a long explanation for this, but I have no
time to explain it here.'"
- JIPsoft
Nov 14 '05 #4
-------------------------------------------------------
I just told you. The rand() function returns a value from 0 to RAND_MAX.
Use the % or & operators to keep it within the range you want.
When you say "a random number of 10", do you mean ten, or 10 in binary,
i.e. two? If it's the former, then you can use rand()%10. If it's the
latter, you can use rand()%2 or rand()&1.
-------------------------------------------------------

That is what I ment sorry.
The base that I was saying is like base of 10 = decimal base o 8 octal
base of 2 binary. I wanted binary.
I think I will loop 16 times the rand()&1
I think I may get 0 and 1s.
Thanks

Nov 14 '05 #5
"Profetas" <xu*****@yahoo. com> wrote in message
news:77******** *************** *******@localho st.talkaboutpro gramming.com...
-------------------------------------------------------
I just told you. The rand() function returns a value from 0 to RAND_MAX.
Use the % or & operators to keep it within the range you want.
When you say "a random number of 10", do you mean ten, or 10 in binary,
i.e. two? If it's the former, then you can use rand()%10. If it's the
latter, you can use rand()%2 or rand()&1.
-------------------------------------------------------

That is what I ment sorry.
The base that I was saying is like base of 10 = decimal base o 8 octal
base of 2 binary. I wanted binary.
I think I will loop 16 times the rand()&1
I think I may get 0 and 1s.
Thanks


int i, r, s;
randomize();
r = rand();
for (i=0; i < 16; i++) /* this could be a while loop */
{
s = (r >> 1) & 1;
}

This is untested but I think it's what you're trying to do.
Nov 14 '05 #6
Profetas <xu*****@yahoo. com> scribbled the following:
-------------------------------------------------------
I just told you. The rand() function returns a value from 0 to RAND_MAX.
Use the % or & operators to keep it within the range you want.
When you say "a random number of 10", do you mean ten, or 10 in binary,
i.e. two? If it's the former, then you can use rand()%10. If it's the
latter, you can use rand()%2 or rand()&1.
------------------------------------------------------- That is what I ment sorry.
The base that I was saying is like base of 10 = decimal base o 8 octal
base of 2 binary. I wanted binary.
I think I will loop 16 times the rand()&1
I think I may get 0 and 1s.
Thanks


Now look here, the same numbers work in any base. Decimal, octal,
binary, or whatever. If you want 8-bit random numbers, just use
rand()%256 or rand()&255. They're all stored in the same way in the
computer memory no matter what base you think of them as.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"And according to Occam's Toothbrush, we only need to optimise the most frequent
instructions."
- Teemu Kerola
Nov 14 '05 #7
I would simply define a char: char b = rand(); i think this should be
enough.

Currently i have the problem that i want to have a true 32 bit random
number with rand(). How can i do that, RAND_MAX is not changeable....
Nov 14 '05 #8
On 2004-05-20, Joona I Palaste <pa*****@cc.hel sinki.fi> wrote:
Now an 8 bit number has values from 0 to 255. Therefore you only need to
"cut down" the number returned by rand() in either of two ways: you can
modulo it by 256 (%256) or bitwise-AND it with 255 (&255).


As an aside, this probably is not the best way to generate 8-bit random
numbers: see http://www.eskimo.com/~scs/C-faq/q13.16.html

(int)((double)r and() / ((double)RAND_M AX + 1) * 256) is what the FAQ
recommends.

Alok
Nov 14 '05 #9
Alok Singhal <al**********@h otmail.com> scribbled the following:
On 2004-05-20, Joona I Palaste <pa*****@cc.hel sinki.fi> wrote:
Now an 8 bit number has values from 0 to 255. Therefore you only need to
"cut down" the number returned by rand() in either of two ways: you can
modulo it by 256 (%256) or bitwise-AND it with 255 (&255).
As an aside, this probably is not the best way to generate 8-bit random
numbers: see http://www.eskimo.com/~scs/C-faq/q13.16.html (int)((double)r and() / ((double)RAND_M AX + 1) * 256) is what the FAQ
recommends.


Note that the C standard does not specify any specific PRNG algorithm.
The FAQ is right for most implementations , but there could well be some
implementation where rand()%256 (or rand()&255, seeing as they're
equivalent) would work just as well.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The obvious mathematical breakthrough would be development of an easy way to
factor large prime numbers."
- Bill Gates
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
7111
by: Ben Justice | last post by:
For a program in c, I need some random numbers for a system were people are placing bets. This is not a commerical project btw. Generally, I tend to rely on things from the standard library, because they're written by people with skills far above mine. Hence, I've always used rand() and FALSELY assumed it could produce unpredictable random numbers and could be used in many situations even if security was an issue. Firstly, lets assume...
46
3224
by: RoSsIaCrIiLoIA | last post by:
Write a function that gets an array of unsigned int fill it with random values all differents, and sorts it. It should be faster than qsort too. Do you like my solution? _______________________ #include <stdio.h> #include <stdlib.h> #include <time.h>
8
4382
by: Jack | last post by:
When I use rand(), is the RAND_MAX value how long I am guaranteed that the same value will not appear twice? And is this a floating window? For example, if RAND_MAX is 32767, and I make 500,000 consecutive rand calls then is the rand() algorithm going to guarantee me that no floating window of calls over that 500,000 rand calls will have the same value twice? The only way that would work is if the series was identical everytime.
5
2312
by: ds | last post by:
Hi all, rand() is not thread safe, a fact that may not be so bad after all.. However, I face the following problem: a piece of code uses rand() to get a random sequence, but always seeds with the same seed for reproducibility of the results. Now I am porting this (old C89) code and have setup a nice app with threads that drives on one thread the old code and on another the new code, so that I can compare the results and see that nothing...
15
3928
by: Rich Fife | last post by:
Quick rand() question: I know you're not supposed to use "rand() % 1024" for instance, because it focuses on the lower bits. However, it seems to me that given that the argument is not a power of two (or near a power of two), that this is not an issue. The upper bits will participate equally in the result with the lower. Am I missing something? Thanks!
0
7985
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7896
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8389
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8263
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5848
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3896
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1503
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1246
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.