|
Since (from rand() as cplusplus.com[^]):
Notice though that this modulo operation does not generate a truly uniformly distributed random number in the span (since in most cases lower numbers are slightly more likely), but it is generally a good approximation for short spans.
An alternative is:
int n = 1 + 6.0 * rand() / (RAND_MAX + 1.0);
Veni, vidi, vici.
|
|
|
|
|
CPallini wrote: Notice though that this modulo operation does not generate a truly uniformly distributed random number in the span (since in most cases lower numbers are slightly more likely), but it is generally a good approximation for short spans.
That's wrong.
I startet a small programm generating one billion random number for each of the two solutions repeatly.
The are no difference and the reason is obvious.
Of course smaller numbers are more likely, but it doesn't matter how big a number is when you use modulo as it depends on divisibility. And there is no difference for small or large numbers.
------------------------------
Author of Primary ROleplaying SysTem
How do I take my coffee? Black as midnight on a moonless night.
War doesn't determine who's right. War determines who's left.
|
|
|
|
|
It depends on the goodness of the pseudo random generator , of course (hint: it is not the reminder function that is 'broken').
The test depends also on the periodicity of the random generator.
How did you perform the test?
Veni, vidi, vici.
|
|
|
|
|
CPallini wrote: How did you perform the test?
I had just two arrays of long integer, filled with random numbers. Then I calculated the difference between the two, printed them and repeated the test, adding the differences.
Of course there are differences but they are neglectable and there is no scheme that they will raise with higher numbers.
------------------------------
Author of Primary ROleplaying SysTem
How do I take my coffee? Black as midnight on a moonless night.
War doesn't determine who's right. War determines who's left.
|
|
|
|
|
This should do it.
<pre>int randomizedValueInRange = (rand()%(maxRange-minRange+1))+minRange;
You talk about Being HUMAN. I have it in my name
AnsHUMAN
|
|
|
|
|
The code is as follows : int random_number = (rand() % 6) + 1;
Example of use:
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std ;
int main(void)
{
srand(static_cast<unsigned>(time(0)));
cout<<" \nDICE GAME\n";
int random_number = (rand() % 6) + 1;
cout<<" \nYou threw a ["<<random_number <<"]\n\n";
return 0;
}
|
|
|
|
|
Typically these requests for code result in a lot of sarcastic responses, and being down voted into oblivion what makes one different?
|
|
|
|
|
Where did you see a sarcastic reply in this thread?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
I suppose he is wondering why there are not.
Veni, vidi, vici.
|
|
|
|
|
I didn't, that's why I'm curious, no 'google it' posts, no links to 'how to ask a question' specifically the part about not doing others' homework, etc... So I asked what makes this post unique?
|
|
|
|
|
Good mood?
I don't know, really.
Veni, vidi, vici.
|
|
|
|
|
Wellness Wednesday?
|
|
|
|
|
I got an application MFC and openGL are used. When I run the application it should load a 3D graphics model. But my tool bar, menu bar everything is coming properly but the 3D model window with the model is not appearing. There is no error or no dll problem. Everything linked propely. Why the 3D model is not coming. Please help me someone. Thanks Sujan
Please check this link for the screen shot
https://www.dropbox.com/sh/5thffcacwx7ye6u/i_mmqIgerU?m[^]
|
|
|
|
|
Is there any chance you have tried to debug this application?
Chris Meech
I am Canadian. [heard in a local bar]
In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]
posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]
|
|
|
|
|
Could it be happened becasue of graphics card. May be the graphics card is not supporting the 3D model or it is not sufficient. Because in some systems it is working fine.
|
|
|
|
|
The question posted by Chris is asking whether you have used your debugger to try and find out what is happening in your program. From the information you have provided (including that picture) we have nothing that would give any clues.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
i tried using debugger. All calls are fine. I am passing the 3D model's file name to the function draw() and then its being called. But nothing is drawn on the screen. pls help.
|
|
|
|
|
I'm sorry but we cannot begin to guess what is happening in your program. You need to find out what is happening in your drawing function and whether it is reading the file and processing its content correctly.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
I have built a unit tester in C++. Compiles and runs under VS and linux (Ubuntu 10). (I know, there are others out there. Spare time project of mine over the years)
I just finished the port to Linux. However, if I throw an exception anywhere, within the shared object that contains the test case it will signal SIGABRT. All my searching has not revealed an answer. I figure there is something messed up in the compile switches.
I reduced it to a very simple occurence
void UTL_STR_RTRIM_1() {
try {
int x = 99;
throw x;
}
catch (int i) {
mr_cout << L("Caught int within test case ") << i << std::endl;
}
catch (...) {
mr_cout << L("Caught ... within test case ") << std::endl;
}
TEST_EQUAL(this, mr_utils::mr_string(L("This is a str")), mr_utils::TrimRight(L("This is a str \t\n\r\0")));
}
So even, throwing and catching and int (tried it with other types and exceptions) will cause SIGABRT
Here is my make file for the shared object that is loaded (with above code) followed by makefile of executable that loads the shared object of test cases. Any ideas?
#
# 'make depend' uses makedepend to automatically generate dependencies
# (dependencies are added to end of Makefile)
# 'make' build static lib 'libutils.a'
# 'make clean' removes all .o and static lib files
#
# define the C compiler to use
CC = gcc
# define any compile-time flags
CFLAGS = -c
CFLAGS = -g
CFLAGS += -fPIC
CFLAGS += -fno-stack-protector
# define any directories containing header files other than /usr/include
INCLUDES =-Iinclude
INCLUDES +=-ICppTestCaseTests
INCLUDES +=-I../CppTestUtils/include
INCLUDES +=-I../CppTest/include
#INCLUDES +=-ICore/include
#INCLUDES +=-IDlls/include
#INCLUDES +=-IExceptions/include
#INCLUDES +=-IFactories/include
#INCLUDES +=-ILogging/include
#INCLUDES +=-IOutputs/include
#INCLUDES +=-IScriptReader/include
# define library paths in addition to /usr/lib
# specify path using -Lpath, something like:
#LFLAGS = -L/home/newhall/lib -L../lib
# define the CPP source files
SRCS = \
source/dllmain.cpp \
# define the CPP object files
#
# This uses Suffix Replacement within a macro:
# $(name:string1=string2)
# For each word in 'name' replace 'string1' with 'string2'
# Below we are replacing the suffix .cpp of all words in the macro SRCS
# with the .o suffix
OBJS = $(SRCS:.cpp=.o)
# folder for lib file.
LIBDIR = lib
# define the shared object file
UTILLIB = $(LIBDIR)/libmr_testTestCases.so
DEFINES+=-DMR_USE_WIDE_STR
DEFINES+=-DDEBUG
# User defined function for compile to make for each compact.
do-compile=$(CC) $(DEFINES) $(CFLAGS) $(INCLUDES) -c $1.cpp -o $1.o > stderr
#do-compile=$(CC) $(DEFINES) $(CFLAGS) $(INCLUDES) -c $1.cpp -o $1.o > stderr
# The following part of the makefile is generic; it can be used to
# build any static lib just by changing the definitions above and by
# deleting dependencies appended to the file from 'make depend'
.PHONY: depend clean
all: $(UTILLIB)
@echo Simple compiler named libmrtest.so has been compiled
# rule to archive all of the object files into the static lib
# chains to the OBJS macro which calls the .cpp.o to do the compilation first.
# For some reason gcc does not send out text to stdout of stderr. Must echo
# it
$(UTILLIB): $(SRCS)
mkdir -p $(LIBDIR)
@echo ------------------------------------------------------------
@echo Compiling object files.
$(foreach i,$(SRCS:.cpp=),$(shell $(call do-compile,$i) ))
@echo ------------------------------------------------------------
#@echo ------------------------------------------------------------
#@echo Creating lib
#$(AR) $(ARFLAGS) $@ $(OBJS)
#@echo ------------------------------------------------------------
@echo ------------------------------------------------------------
@echo Creating Shared Object
ld -shared -soname $@ -o $@ $(OBJS)
# $(CC) -shared -W1,-soname $@ -o $@ -fPIC $(SRCS)
# ld -shared -init __nonWinCutTestCaseRegistrationMethod__ -soname $@ -o $@ $(OBJS)
# $(CC) -Wall -shared -fPIC -W1,soname $@ -o $@ $(INCLUDES) $(SRCS)
# $(CC) -shared -W1,-soname,$@ -o $@ $(OBJS)
# $(CC) -Wall -shared -fPIC -W1,-soname,$@ -o $@ $(OBJS)
#this one may actually call the attr constructor on dll. but undefined symbols found
# $(CC) -Wall -shared -fPIC -o $@ $(INCLUDES) $(SRCS)
@echo ------------------------------------------------------------
# ld -shared -soname $@ -o $@ /usr/lib/gcc/i686-linux-gnu/4.6.3/libgcc_s.so -lc -fno-stack-protector $(OBJS)
# ld -shared -fno-stack-protector -soname $@ -o $@ $(OBJS)
#ld -shared -lc -fno-stack-protector -soname $@ -o $@ $(OBJS)
# TODO - lookup what the no-stack-protector might be doing
# this is a suffix replacement rule for building .o's from .cpp's
# it uses automatic variables $<: the name of the prerequisite of
# the rule(a .cpp file) and $@: the name of the target of the rule (a .o file)
# (see the gnu make manual section about automatic variables)
#.cpp.o:
# $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
clean:
@echo ------------------------------------------------------------
@echo Cleaning House
@echo ------------------------------------------------------------
$(RM) $(UTILLIB)
$(RM) $(OBJS)
depend: $(SRCS)
makedepend $(INCLUDES) $^
# DO NOT DELETE THIS LINE -- make depend needs it
Executable make file
#
# 'make depend' uses makedepend to automatically generate dependencies
# (dependencies are added to end of Makefile)
# 'make' build static lib 'libutils.a'
# 'make clean' removes all .o and static lib files
#
# define the C compiler to use
CC = gcc
# define any compile-time flags
CFLAGS = -c
CFLAGS = -g
CFLAGS += -fPIC
CFLAGS += -fno-stack-protector
# define any directories containing header files other than /usr/include
INCLUDES +=-I../CppTestUtils/include
INCLUDES +=-I../CppTest/include
INCLUDES +=-I../CppTest/Core/include
# define library paths in addition to /usr/lib
# specify path using -Lpath, something like:
#LFLAGS = -L/home/newhall/lib -L../lib
# define the CPP source files
SRCS = \
source/CppTestDevConsole.cpp \
# define the CPP object files
#
# This uses Suffix Replacement within a macro:
# $(name:string1=string2)
# For each word in 'name' replace 'string1' with 'string2'
# Below we are replacing the suffix .cpp of all words in the macro SRCS
# with the .o suffix
OBJS = $(SRCS:.cpp=.o)
# folder for lib file.
LIBDIR = lib
# define the shared object file
UTILLIB = $(LIBDIR)/cutConsoleRunner
# the libs from the other Cut so
#LIBS+=-lstdc++
#LIBS+=-lcut_utils
#LIBS+=-ldl
#LIBS+=-lcut_test
LIBS+=-lcut_test
LIBS+=-lcut_utils
LIBS+=-ldl
LIBS+=-lstdc++
#until I can get groups and rights setup
#LIBDIRS+=-L/home/michael/opt/cut_test/lib
#LIBDIRS+=-L/usr/lib/gcc/i686-linux-gnu/4.6.3
LIBDIRS+=-L/usr/lib/gcc/i686-linux-gnu/4.6.3/
LIBDIRS+=-L/opt/cut_test/lib/
DEFINES+=-DMR_USE_WIDE_STR
DEFINES+=-DDEBUG
# User defined function for compile to make for each compact.
do-compile=$(CC) $(DEFINES) $(CFLAGS) $(INCLUDES) -c $1.cpp -o $1.o > stderr
#do-compile=$(CC) $(DEFINES) $(CFLAGS) $(INCLUDES) -c $1.cpp -o $1.o > stderr
# The following part of the makefile is generic; it can be used to
# build any static lib just by changing the definitions above and by
# deleting dependencies appended to the file from 'make depend'
.PHONY: depend clean
all: $(UTILLIB)
@echo Simple compiler named ConsoleRunner has been compiled
# rule to archive all of the object files into the static lib
# chains to the OBJS macro which calls the .cpp.o to do the compilation first.
# For some reason gcc does not send out text to stdout of stderr. Must echo
# it
$(UTILLIB): $(SRCS)
mkdir -p $(LIBDIR)
@echo ------------------------------------------------------------
@echo Compiling object files.
$(foreach i,$(SRCS:.cpp=),$(shell $(call do-compile,$i) ))
@echo ------------------------------------------------------------
@echo ------------------------------------------------------------
@echo Objects $(OBJS)
@echo Libs $(LIBS)
@echo ------------------------------------------------------------
@echo ------------------------------------------------------------
@echo Linking into executable
# ld -o $@ $(OBJS) $(LIBDIRS) $(LIBS)
# ld -export-dynamic $(OBJS) $(LIBDIRS) $(LIBS) -o $@
# $(CC) -o $@ $(OBJS) $(LIBDIRS) $(LIBS)
# $(CC) $(OBJS) $(LIBDIRS) $(LIBS) -o $@
$(CC) -export-dynamic $(OBJS) $(LIBDIRS) $(LIBS) -o $@
@echo ------------------------------------------------------------
#$(LDFLAGS) $(CUT_LIBS)
# @echo ------------------------------------------------------------
# @echo Creating Shared Object
# ld -shared -soname $@ -o $@ $(OBJS)
# @echo ------------------------------------------------------------
# ld -shared -soname $@ -o $@ /usr/lib/gcc/i686-linux-gnu/4.6.3/libgcc_s.so -lc -fno-stack-protector $(OBJS)
# ld -shared -fno-stack-protector -soname $@ -o $@ $(OBJS)
#ld -shared -lc -fno-stack-protector -soname $@ -o $@ $(OBJS)
# TODO - lookup what the no-stack-protector might be doing
# this is a suffix replacement rule for building .o's from .cpp's
# it uses automatic variables $<: the name of the prerequisite of
# the rule(a .cpp file) and $@: the name of the target of the rule (a .o file)
# (see the gnu make manual section about automatic variables)
#.cpp.o:
# $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
clean:
@echo ------------------------------------------------------------
@echo Cleaning House
@echo ------------------------------------------------------------
$(RM) $(UTILLIB)
$(RM) $(OBJS)
depend: $(SRCS)
makedepend $(INCLUDES) $^
# DO NOT DELETE THIS LINE -- make depend needs it
|
|
|
|
|
Where does the SIGABRT get raised? You may need to add your own signal handler to trap it and take action accordingly.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
It is raised at the point where the exception is thrown. As you can see, unless I am totally missing something, the int is being thrown and properly caught so there should not be a signal raised at all. This is my dilema.
I put in a signal handler but it does no good. App still closes. I will need to handle the signals eventually since this is a unit tester. But for now, I am just trying to figure out why the SIGABRT could possibly be raised on a legitimate thrown and caught exception
This is the message at runtime:
terminate called after throwing an instance of 'int'
The program has unexpectedly finished.
Basically it acts as if there is no catch and the terminate is called. Or perhaps I am misreading something. The stack dump is below
0 __kernel_vsyscall 0 0xb7fdd424
1 __GI_raise raise.c 64 0xb7bd61ef
2 __GI_abort abort.c 91 0xb7bd9835
3 __gnu_cxx::__verbose_terminate_handler() /usr/lib/i386-linux-gnu/libstdc++.so.6 0 0xb7e1813d
4 ?? /usr/lib/i386-linux-gnu/libstdc++.so.6 0 0xb7e15ed3
5 std::terminate() /usr/lib/i386-linux-gnu/libstdc++.so.6 0 0xb7e15f0f
6 __cxa_throw /usr/lib/i386-linux-gnu/libstdc++.so.6 0 0xb7e1605e
7 UtilStrTrimTests::UTL_STR_RTRIM_1 mr_util_str_trim_tests.cpp 31 0xb7b6d692
8 MrTest::Fixture::ExecStep CppTestFixture.cpp 178 0xb7f56d2a
9 MrTest::Fixture::RunTest CppTestFixture.cpp 160 0xb7f56c3c
10 MrTest::EngineImplementation::RunAllFixtureTests MrTestEngineImplementation.cpp 350 0xb7f71a89
11 MrTest::EngineImplementation::ProcessTestList MrTestEngineImplementation.cpp 227 0xb7f711fc
12 MrTest::Engine::ProcessTestList CppTestEngine.cpp 57 0xb7f48241
13 main CppTestDevConsole.cpp 94 0x804cb5c
|
|
|
|
|
I found a solution to the SIGABRT problem. As I suspected it was a problem with the makefile. In particular, I changed
this:
ld -shared -soname $@ -o $@ $(OBJS)
to this:
$(CC) -shared -W1,-soname,$@ -o $@ $(OBJS)
It now behaves in the same way as in Windows. As long as I catch the exception properly it will never SIGABRT.
Seeing that I am not a linux guru, I would appreciate if someone out there has an explanation why a shared object linked with ld will not allow exceptions while one linked with gcc will.
It may also help anyone else having a similar problem. I looked far and wide and could find no documentation on this.
|
|
|
|
|
Can anyone please explain me how to build and use cppunit in visual studio 2010?
I have tried a lot but not able to get it..
|
|
|
|
|
Not sure if this will help, but look at this article[^].
Chris Meech
I am Canadian. [heard in a local bar]
In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]
posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]
|
|
|
|
|